Thinking About How Visual Studio .NET Uses a WSDL Document

A web service consists of one or more methods. Before your program can use the web service, your program must know specifics about each of the program’s callable methods. When you add a web reference to your program, Visual Studio .NET parses the WSDL document to determine specifics about the service methods. In addition, Visual Studio .NET inserts behind-the-scenes program instructions into your program code that handle the network message passing between the program and the web service and the shuffling of data values to and from the method parameters. Programmers refer to program instructions as the proxy code. Visual Studio .NET implements the code using the proxy class. When your program calls a web service method, your code actually calls the proxy class object, which in turn accesses the remote web service.

When you build a program within Visual Studio .NET, the compiler will insert the proxy class code into your application automatically after you add a web reference for the web service. If you are interested in seeing the actual proxy class code, you can use the WSDL.exe provided within Visual Studio .NET to generate the proxy-class source code. The following WSDL command, for example, generates the proxy-class source code, using C#, for the DateServer web service:

C:\> wsdl /l:CS http://localhost/DateService/Service1.asmx  <Enter>

In the case of the DateService, the WSDL command will create the proxy-class source code shown in Listing 4.6.

Listing 4.6 Service1.vb

start example
//--------------------------------------------------------- // <autogenerated> //     This code was generated by a tool. //     Runtime Version: 1.0.3705.0 // //     Changes to this file may cause incorrect behavior and will be //     lost if the code is regenerated. // </autogenerated> //--------------------------------------------------------- // // This source code was auto-generated by wsdl, Version=1.0.3705.0. // using System.Diagnostics; using System.Xml.Serialization; using System; using System.Web.Services.Protocols; using System.ComponentModel; using System.Web.Services; /// <remarks/> [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Web.Services.WebServiceBindingAttribute Ä  (Name="Service1Soap", Namespace="http://tempuri.org/")] public class Service1 : System.Web.Services.Protocols.SoapHttpClientProtocol {        /// <remarks/>     public Service1() {         this.Url = "http://localhost/DateService/Service1.asmx";     }        /// <remarks/>     [System.Web.Services.Protocols.SoapDocumentMethodAttribute( Ä"http://tempuri.org/DateString",  Ä   RequestNamespace="http://tempuri.org/",  Ä   ResponseNamespace="http://tempuri.org/",  Ä   Use=System.Web.Services.Description.SoapBindingUse.Literal, Ä ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]     public string DateString() {         object[] results = this.Invoke("DateString", new object[0]);         return ((string)(results[0]));     }        /// <remarks/>     public System.IAsyncResult BeginDateString(System.AsyncCallback Ä   callback, object asyncState) {         return this.BeginInvoke("DateString", new object[0], callback, Ä       asyncState);     }        /// <remarks/>     public string EndDateString(System.IAsyncResult asyncResult) {         object[] results = this.EndInvoke(asyncResult);         return ((string)(results[0]));     }        /// <remarks/>     [System.Web.Services.Protocols.SoapDocumentMethodAttribute( Ä"http://tempuri.org/TimeString",  Ä   RequestNamespace="http://tempuri.org/",  Ä   ResponseNamespace="http://tempuri.org/",  Ä   Use=System.Web.Services.Description.SoapBindingUse.Literal,  ÄParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]     public string TimeString() {         object[] results = this.Invoke("TimeString", new object[0]);         return ((string)(results[0]));     }        /// <remarks/>     public System.IAsyncResult BeginTimeString(System.AsyncCallback Ä   callback, object asyncState) {         return this.BeginInvoke("TimeString", new object[0], callback, Ä       asyncState);     }        /// <remarks/>     public string EndTimeString(System.IAsyncResult asyncResult) {         object[] results = this.EndInvoke(asyncResult);         return ((string)(results[0]));     }        /// <remarks/>     [System.Web.Services.Protocols.SoapDocumentMethodAttribute( Ä"http://tempuri.org/DayOfWeek",  Ä   RequestNamespace="http://tempuri.org/",  Ä   ResponseNamespace="http://tempuri.org/",  Ä   Use=System.Web.Services.Description.SoapBindingUse.Literal,  ÄParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]     public int DayOfWeek() {         object[] results = this.Invoke("DayOfWeek", new object[0]);         return ((int)(results[0]));     }        /// <remarks/>     public System.IAsyncResult BeginDayOfWeek(System.AsyncCallback Ä   callback, object asyncState) {         return this.BeginInvoke("DayOfWeek", new object[0],  Ä      callback, asyncState);     }        /// <remarks/>     public int EndDayOfWeek(System.IAsyncResult asyncResult) {         object[] results = this.EndInvoke(asyncResult);         return ((int)(results[0]));     } }
end example

For now, do not concern yourself with the specific processing the proxy performs. Instead, note that the proxy provides support for HTTP- and SOAP-based interactions with the service. In addition, the proxy provides support for synchronous and asynchronous method calls. Throughout this book’s first chapters, your programs have called web service methods synchronously, meaning the program calls the remote method and suspends its processing until the method returns a value. In later chapters, you will learn how to call web service methods asynchronously, so that the program can continue its processing in parallel within

the service. After the service completes its processing and returns a result, the program will retrieve and process the result.

How Programmers Can Use the Proxy-Class Source Code

Because most programmers will create programs that use web services from within Visual Studio .NET, most will never have to worry about a web service’s proxy-class code. However, some “old school” programmers just can’t seem to get enough of the command line and still build their programs using command-line utilities. Such programmers must include the source code for the proxy class when they compile programs that use web services.

To better understand the command-line compilation process, create the C# console-based application shown in Listing 4.7, UseDateService.cs, that uses the web service’s methods.

Listing 4.7 UseDateService.cs

start example
using System; class UseDateService {    static void Main()    {       Service1 Dt = new Service1();       Console.WriteLine("Today is: " + Dt.DateString());       Console.WriteLine("Time is: " + Dt.TimeString());    } }
end example

To compile the program, you would use the following command line:

C:\> csc UseDateService.cs Service1.cs  <Enter>

Then, to run the program, you would use the following command:

C:\> UseDateService  <Enter> Today is: 01/26/2003 Time is: 14:02:32.6284880




. NET Web Services Solutions
.NET Web Services Solutions
ISBN: 0782141722
EAN: 2147483647
Year: 2005
Pages: 161
Authors: Kris Jamsa

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