Section 15.8. Creating the Proxy


15.8. Creating the Proxy

Before you can create a client application to interact with the calculator web service, you will first create a proxy class. Once again, you can do this by hand, but that would be hard work. The folks at Microsoft have provided a tool called wsdl that generates the source code for the proxy based on the information in the WSDL file.

To create the proxy, enter wsdl at the Visual Studio command-line prompt, followed by the path to the WSDL contract. For example, you might enter:

wsdl http://localhost:19975/CalculatorWS/Service.asmx?wsdl

The result is the creation of a C# client file named Service1.cs, an excerpt of which appears in Example 15-5.

Example 15-5. Sample client code to access the calculator web service
/---------------------------------------------------------------- // <autogenerated> //     This code was generated by a tool. //     Runtime Version:2.0.40607.16 // //     Changes to this file may cause incorrect behavior and  //     will be lost if the code is regenerated. // </autogenerated> //---------------------------------------------------------------- using System; using System.ComponentModel; using System.Diagnostics; using System.Web.Services; using System.Web.Services.Protocols; using System.Xml.Serialization; //  // This source code was auto-generated by wsdl, Version=2.0.40607.16. //  /// <remarks/> [System.Diagnostics.DebuggerStepThroughAttribute( )] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Web.Services.WebServiceBindingAttribute(Name="ServiceSoap",  Namespace="http://tempuri.org/")] public class Service : System.Web.Services.Protocols.SoapHttpClientProtocol {          private System.Threading.SendOrPostCallback AddOperationCompleted;          private System.Threading.SendOrPostCallback SubOperationCompleted;          private System.Threading.SendOrPostCallback MultOperationCompleted;          private System.Threading.SendOrPostCallback DivOperationCompleted;          private System.Threading.SendOrPostCallback PowOperationCompleted;          /// <remarks/>     public Service( ) {         this.Url = "http://localhost:19975/CalculatorWS/Service.asmx";     }          /// <remarks/>     public event AddCompletedEventHandler AddCompleted;          /// <remarks/>     public event SubCompletedEventHandler SubCompleted;          /// <remarks/>     public event MultCompletedEventHandler MultCompleted;          /// <remarks/>     public event DivCompletedEventHandler DivCompleted;          /// <remarks/>     public event PowCompletedEventHandler PowCompleted;          /// <remarks/>     [System.Web.Services.Protocols.SoapDocumentMethodAttribute   ("http://tempuri.org/Add", RequestNamespace="http://tempuri.org/",    ResponseNamespace="http://tempuri.org/",    Use=System.Web.Services.Description.SoapBindingUse.Literal,    ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]     public double Add(double x, double y) {         object[] results = this.Invoke("Add", new object[] {                     x,                     y});         return ((double)(results[0]));     }          /// <remarks/>     public System.IAsyncResult BeginAdd(double x, double y, System.            AsyncCallback callback, object asyncState) {         return this.BeginInvoke("Add", new object[] {                     x,                     y}, callback, asyncState);     }          /// <remarks/>     public double EndAdd(System.IAsyncResult asyncResult) {         object[] results = this.EndInvoke(asyncResult);         return ((double)(results[0]));     }          /// <remarks/>     public void AddAsync(double x, double y) {         this.AddAsync(x, y, null);     }          /// <remarks/>     public void AddAsync(double x, double y, object userState) {         if ((this.AddOperationCompleted == null)) {             this.AddOperationCompleted = new                System.Threading.SendOrPostCallback(                 this.OnAddOperationCompleted);         }         this.InvokeAsync("Add", new object[] {                     x,                     y}, this.AddOperationCompleted, userState);     }          private void OnAddOperationCompleted(object arg) {         if ((this.AddCompleted != null)) {             System.Web.Services.Protocols.               InvokeCompletedEventArgs invokeArgs =                 ((System.Web.Services.Protocols.InvokeCompletedEventArgs)                (arg));             this.AddCompleted(this,               new AddCompletedEventArgs(invokeArgs.Results,               invokeArgs.Error, invokeArgs.Cancelled,                invokeArgs.UserState));         }     }          /// <remarks/>     //...

This complex code is produced by the WSDL tool to build the proxy DLL you will need when you build your client. The file uses attributes extensively, but with your working knowledge of C# you can extrapolate at least how some of it works.

The file starts by declaring the Service1 class that derives from the SoapHttp-ClientProtocol class, which occurs in the namespace called System.Web.Services.Protocols:

public class Service1 :     System.Web.Services.Protocols.SoapHttpClientProtocol

The constructor sets the URL property inherited from SoapHttpClientProtocol to the URL of the .asmx page you created earlier.

The Add() method is declared with a host of attributes that provide the SOAP plumbing to make the remote invocation work.

The WSDL application has also provided asynchronous support for your methods. For example, for the Add() method, it also created BeginAdd() and EndAdd( ). This allows you to interact with a web service without performance penalties.

15.8.1. Testing the Web Service

To test the web service, create a simple C# console application. Right-click the project, and add the Service1.cs file you created from the console window. Visual Studio will create a proxy for you named theWebSvc .

This done, you can invoke the Pow() method as if it were a method on a locally available object:

for (int i = 2;i<10; i++)     for (int j = 1;j <10;j++)     {        Console.WriteLine(          "{0} to the power of {1} = {2}", i, j,         theWebSvc.Pow(i, j));     }

This simple loop creates a table of the powers of the numbers 2 through 9, displaying for each the powers 1 tHRough 9. The complete source code and an excerpt of the output is shown in Example 15-6.

Example 15-6. A client program to test the calculator web service
#region Using directives using System; using System.Collections.Generic; using System.Text; #endregion namespace CalculatorTest {    class Program    { // driver program to test the web service       public class Tester       {          public static void Main( )          {             Tester t = new Tester( );             t.Run( );          }          public void Run( )          {             int var1 = 5;             int var2 = 7;             // instantiate the web service proxy             Service theWebSvc = new Service( );             // call the add method             Console.WriteLine( "{0} + {1} = {2}", var1, var2,                theWebSvc.Add( var1, var2 ) );             // build a table by repeatedly calling the pow method             for ( int i = 2; i < 10; i++ )                for ( int j = 1; j < 10; j++ )                {                   Console.WriteLine( "{0} to the power of {1} = {2}",                       i, j, theWebSvc.Pow( i, j ) );              }          }       }    } } Output (excerpt): 5 + 7 = 12 2 to the power of 1 = 2 2 to the power of 2 = 4 2 to the power of 3 = 8 2 to the power of 4 = 16 2 to the power of 5 = 32 2 to the power of 6 = 64 2 to the power of 7 = 128 2 to the power of 8 = 256 2 to the power of 9 = 512 3 to the power of 1 = 3 3 to the power of 2 = 9 3 to the power of 3 = 27 3 to the power of 4 = 81 3 to the power of 5 = 243 3 to the power of 6 = 729 3 to the power of 7 = 2187 3 to the power of 8 = 6561 3 to the power of 9 = 19683



Programming C#(c) Building. NET Applications with C#
Programming C#: Building .NET Applications with C#
ISBN: 0596006993
EAN: 2147483647
Year: 2003
Pages: 180
Authors: Jesse Liberty

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