Create a new ASP.NET Web Service Application. You can use the solution and project name as in the following Figure.
Add the following code.
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Linq; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; using System.Xml.Linq;
namespace WeatherServiceCS { /// <summary> /// Summary description for Service1 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [ToolboxItem(false)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. // [System.Web.Script.Services.ScriptService]
// This class exposes a Web Service method that // takes a zip code and returns the forecast // for that area. public class Service1 : System.Web.Services.WebService { // The logic for GetTodaysForecast is limited for the // purposes of this example to check for // zip = 11111 and return "rainy" if it matches, // otherwise it will return "sunny". [WebMethod] public string GetTodaysForecast(string zip) { string forecast = "sunny";
if (zip == "11111") forecast = "rainy";
return forecast; } } } |
Build the project.
Add a web form.
|
Add the following code.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WeatherServiceCS.WebForm1" %> <%@ Import Namespace="WeatherServiceCS" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>A very simple asp-web application</title> </head> <script language="C#" runat="server"> string zipcode = "11111"; public void Submit_Click(Object sender, EventArgs E) { try { // Get the zip code from the form zipcode = ZipCode.Text; } catch (Exception) { /* ignored */ } // Create the weather service that will return the forecast Service1 weatherService = new Service1(); // Set the display with the result of GetTodaysForecast Result.Text = "Today’s forecast is: <b>" + weatherService.GetTodaysForecast(zipcode) + "</b>."; } </script> <body style="font: 10pt verdana"> <h4>Weather Report </h4> <form id="Form1" runat="server">
<div style="padding:15,15,15,15;background-color:Gray; width:300;border-color:black;border-width:1; border-style:solid">
Zip Code: <br/> <asp:TextBox id="ZipCode" Text="11111" runat="server"/><br/>
<input type="submit" id="Add" value="Get Weather Report" OnServerClick="Submit_Click" runat="server"/> <p/> <asp:Label id="Result" runat="server"/> </div> </form> </body> </html> |
You can preview the form by switching to the Design page as shown below.
Run the project.
The following are sample outputs.
OK, so we just called a pretty basic service on a form that ran on the local machine. Now consider the possibility that the weather service resides on the Internet rather than on the local machine and is connected to live weather feeds from around the world. The programming model for calling the “ultimate weather service” in this scenario would be exactly the same as the sample you just saw.