Section 16.2. Creating the Client with VS2005

16.2. Creating the Client with VS2005

The easiest way to create a web service client is to let VS2005 do all the work. This includes creating the proxy for the web service and adding a reference to that proxy to the client application. You will see how to do some or all of the steps outside VS2005 later in this chapter.

Why wouldn't you let VS2005 do all the work, you might ask? By taking control of the process and performing certain steps manually, you gain flexibility and features that may be helpful under certain (admittedly unusual) scenarios. In addition, you gain a more thorough understanding of how web services work and what is happening behind the scenes, so doing it yourself has great geek appeal and impresses your friends at parties.

To start, first create an ultra -simple web service in VS2005. Call it WebServiceSimple . This will bring up the default start-up boilerplate code in the code-behind file with a web method called HelloWorld . Modify this web method a bit to return the current time stamp, as shown highlighted in Example 16-1.

Example 16-1. App_Code\Service.cs for WebServiceSimple
 using System; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class Service : System.Web.Services.WebService {     public Service (  ) {     }     [WebMethod]     public string HelloWorld(  ) {  return "Hello World. The time is " +             DateTime.Now.ToLongTimeString(  );  } } 

After saving this web service, create the consuming web site. Be sure to select the correct template, ASP.NET Web Site, from the New Web Site dialog box. (If the previous web service was the last new web application created, the default template will remain ASP.NET Web Service.) Call this new web site WebServiceSimpleConsumer . When this page runs it will look something like Figure 16-1, with a label displaying a message and two buttons .

Figure 16-1. WebServiceSimpleConsumer

The content file for WebServiceSimpleConsumer is listed in Example 16-2, with the important controls highlighted. A click event handler exists for the Call Web Service button but not the Post Back button. The latter simply posts the form, and the only code that runs is that in Page_Load . You will see the code-behind file in a moment.

Example 16-2. Default.aspx for WebServiceSimpleConsumer
 <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs"     Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"     "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">     <title>Web Service Simple Consumer</title> </head> <body>     <form id="form1" runat="server">     <div>  <h1>Web Service Simple Consumer</h1>  <br />        <asp:Label ID="lblMessage" runat="server" />        <br />        <br />  <asp:Button ID="btnPost" runat="server"                    Text="Post Back" />        <asp:Button ID="btnWebService" runat="server"                    Text="Call Web Service"                    OnClick="btnWebService_Click" />  </div>     </form> </body> </html> 

First, you must provide a reference to the web service. To be more precise, you must provide a reference to a proxy to the web service as was described in the previous chapter and illustrated in Figures 15-1 and 15-3.

This is where VS2005 does its magic, adding a web reference to your client. VS2005 can add a web reference to a web service only if that service is being served from a virtual directory. For web services accessed over the Internet, this is inherently the case. However, to consume the web service you just created on your local machine, you must have IIS installed on the machine and you must create a virtual directory pointing to that web service.

Create the virtual directory in the Computer Management window by right-clicking on My Computer and selecting Manage, or from the Start Control Panel Administrative Tools Computer Management. Drill down to Services and Applications Internet Information Services Web Sites Default Web Site, right-click Default Web Site, and choose New Virtual Directory. Follow the wizard, entering WebServiceSimple as the alias (i.e., the name of the virtual directory) and browsing to the location of the web service file. You can verify that the virtual directory is made correctly by entering the following URL into a browser:

 http://localhost/WebServiceSimple/service.asmx 

You should get the web service test page, as shown in Figure 16-2.

Figure 16-2. WebServiceSimple test page

Once the virtual directory for the web service is created, you can add that web service as a web reference to a consuming web site. To add a reference to a web service to your site, click on the Website Add Web Reference... menu item, or right-click on the project file in the Solution Explorer and select Add Web Reference....In either case, you will get the Add Web Reference dialog box shown in Figure 16-3.

Figure 16-3. Add Web Reference dialog box

If the web service you wished to reference were on a different machine, you would enter the URL of either the web service file ( .asmx ), the .wsdl file, or a discovery file ( .disco ). Then click the Go button. In this example, click on Web services on the local machine. Alternatively, you could search one of several UDDI directories.

In any case, VS2005 will search the specified location and present any available web services in a list of hyperlinks . Click on the web service you want to reference ( Service , in this case). It will then display the test page for that web service, similar to what you saw in Figure 16-2. If this is the web service you want to reference, click on the Add Reference button. That is all there is to it.

VS2005 will add a folder to the project called App_WebReferences , which will display in the Solution Explorer. Under that folder will be another folder named after the web server, localhost in this case. Under that will be three files, all named after the web service file: .disco , .discomap , and .wsdl . These file types have all been described previously.

What you will not see anywhere is either the source code for the proxy class (which you will see when you perform the steps manually later in this chapter) or a compiled proxy assembly. This compiled assembly is there somewhere behind the scenes, but VS2005 manages it.

The reference is made available to the web page at runtime via an entry made automatically in the web.config file. This is an appSetting ( appSettings will be described in Chapter 18) with a key comprised of the server name concatenated with the web service name, as in:

 <appSettings>        <add key="localhost.Service"             value="http://localhost/WebServiceSimple/Service.asmx"/>     </appSettings> 

Once the web reference has been added, your code-behind in the consuming web page can refer to the web service as though it were a class on the local machine.

The complete code-behind file for the consuming web site, WebServiceSimpleConsumer , is listed in Example 16-3.

Example 16-3. Default.aspx.cs for WebServiceSimpleConsumer
 using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class _Default : System.Web.UI.Page {     protected void Page_Load(object sender, EventArgs e)     {        if (!IsPostBack)        {           lblMessage.Text = "First loaded at " +              DateTime.Now.ToLongTimeString(  );        }        else        {           lblMessage.Text = "Postback at " +              DateTime.Now.ToLongTimeString(  );        }     }    protected void btnWebService_Click(object sender, EventArgs e)    {  localhost.Service proxy = new localhost.Service(  );  lblMessage.Text = "Web Service returns: " +  proxy.HelloWorld(  )  ;    } } 

Looking at the Page_Load method, the Label control is populated with a relevant message and the timestamp every time the page posts, either from initial load or clicking the Post Back button.

The line of code in Example 16-3 that instantiates a reference to the web service is highlighted, as well the usage of a web method in the following line. This code is contained in the Click event handler for the Call Web Service button. As you type the server name ( localhost ) in the code window, you will see that Intellisense knows about the class and method names in the web service.



Programming ASP. NET
Programming ASP.NET 3.5
ISBN: 0596529562
EAN: 2147483647
Year: 2003
Pages: 173

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net