Implementing a Windows Client


The test is working, but you want to create a Windows client that uses the Web service. The client must create a SOAP message that will be sent across an HTTP channel. It is not necessary to make this message ourselves. the System.Web.Services.Protocols.SoapHttpClientProtocol class does all the work behind the scenes.

Try It Out – Creating a Client Windows Application

image from book
  1. Create a new C# Windows Application, call it SimpleClient, and add two text boxes and a button to the form (see Figure 20-12). You will use the button's Click handler to invoke the Web service.

    image from book
    Figure 20-12

  2. Add a Web reference using the Project Add Web Reference... menu and enter the URL of the Web service that was just generated. Then, you can view the contract and documentation. Before pressing the Add Reference button, change the Web reference name to WebServicesSample, as shown in Figure 20-13.

    image from book
    Figure 20-13

  3. In the Solution Explorer you can now see a new Web Reference, WebServicesSample. When you click the Show All Files button, you can see the WSDL document and the file reference.cs that includes the source code of the proxy (see Figure 20-14).

    image from book
    Figure 20-14

    What the Solution Explorer shows only when the Show All Files button is clicked can be shown more easily in the Class View (the new class that implements the client proxy). This class converts method calls to the SOAP format. In Class View (see Figure 20-15), you will find a new namespace with the name that was defined with the Web Reference name. In this case, WebServicesSample was created. The class Service derives from SoapHttpClientProtocol and implements the method of the Web service, ReverseString().

    image from book
    Figure 20-15

    Double-click the Service class to open the auto-generated reference.cs file. Let's look into this wizard-generated code.

    The Service class derives from the SoapHttpClientProtocol class. This base class creates a SOAP message in the Invoke() method. the WebServiceBindingAttribute attribute sets binding values to the Web service:

    [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Web.Services.WebServiceBindingAttribute(Name="ServiceSoap",    Namespace="http://www.wrox.com/webservices")] public partial class Service :    System.Web.Services.Protocols.SoapHttpClientProtocol { 

    In the constructor, the Url property is set to the Web service. This property will be used from the SoapHttpClientProtocol class to request a service:

       public Service() {       this.Url =  SimpleClient.Properties.Settings.Default.SimpleClient_WebServicesSample_Service;    }

    The most important method is the method that the Web service supplies: ReverseString(). The method here has the same parameter that you implemented on the server. The implementation of the client-side version of ReverseString() calls the Invoke() method of the base class SoapHttpClientProtocol. Invoke() creates a SOAP message using the method name ReverseString and the parameter message:

    [System.Web.Services.Protocols.SoapDocumentMethodAttribute(    "http://www.wrox.com/webservices/ReverseString",    RequestNamespace="http://www.wrox.com/webservices",    ResponseNamespace="http://www.wrox.com/webservices",    Use=System.Web.Services.Description.SoapBindingUse.Literal,    ParameterStyle=       System.Web.Services.Protocols.SoapParameterStyle.Wrapped)] public string ReverseString(string message) {    object[] results = this.Invoke("ReverseString", new object[]       { message});    return ((string)(results[0])); }

    Until now you have not written a single line of code for the client. You designed a small user interface, and used the Add Web Reference menu to create a proxy class. Now you just have to create the link between the two.

  4. Add a Click event handler to the button and add these two lines of code:

    private void button1_Click(object sender, EventArgs e) { WebServicesSample.Service ws = new WebServicesSample.Service();   textBox2.Text = ws.ReverseString(textBox1.Text); }

How It Works

With the following line, you create a new instance of the proxy class. As you saw in the implementation of the constructor, the Url property is set to the Web service:

WebServicesSample.Service ws = new WebServicesSample.Service();

As a result of calling the ReverseString() method of the proxy class, a SOAP message is sent to the server, and so the Web service is called:

textBox2.Text = ws.ReverseString(textBox1.Text);

Running the program produces output like that shown in Figure 20-16.

image from book
Figure 20-16

image from book




Beginning Visual C# 2005
Beginning Visual C#supAND#174;/sup 2005
ISBN: B000N7ETVG
EAN: N/A
Year: 2005
Pages: 278

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