- Introduction
- Implementing a Basic Web Service in C#
- Exploring the Web Service Proxy Class
- Invoking a Web Service Asynchronously
- Summary
Exploring the Web Service Proxy Class
When you're ready to consume a Web Servicewhether or not you created ityou need to add a web reference to your project. Using UDDI and Visual Studio .NET to add a web reference will generate a proxy file named Reference.cs; this file makes it very easy to invoke Web Services.
TIP
You can generate proxy files manually by typing a command from the Visual Studio .NET command prompt (WSDL.exe needs to be in your file path):
WSDL http://host/service.asmx
For example:
WSDL http://localhost/AirportInformation/Service1.asmx
WSDL generates a local proxy class for your Web Service and the types that your Web Services exports. For example, our strongly typed collection of Airport objects is exported as an array of Airport objects and a proxy class for the AirportLocatorLibrary.Airport class is created. Proxy classes are created for exported types because no consumer can be expected to have the assemblies containing the original code when they use a Web Service.
NOTE
Managing proxy classes is fodder for another discussion, such as can be found in my book Advanced C# Programming (Osborne/McGraw-Hill, 2002).
We're interested in the proxy class for our Web Service. For the Web Service class itself, WSDL generates three proxy methods for each web method. For example, we defined GetAirports, so WSDL will generate proxies named GetAirports, BeginGetAirports, and EndGetAirports. The first method is the synchronous version of our method and the other two represent the methods we call to perform asynchronous invocation. Listing 3 shows an excerpt from Reference.cs, showing the two proxy methods for asynchronous Web Service invocation.
Listing 3Two Proxy Methods for Asynchronous Web Service Invocation
public System.IAsyncResult BeginGetAirports( string state, System.AsyncCallback callback, object asyncState) { return this.BeginInvoke("GetAirports", new object[] {state}, callback, asyncState); } public Airport[] EndGetAirports( System.IAsyncResult asyncResult) { object[] results = this.EndInvoke(asyncResult); return ((Airport[])(results[0])); }
BeginGetAirports is called to make the asynchronous call and EndGetAirports can be called when the data is ready. The callback delegate passed to BeginGetAirports will tell us when the data is ready, and that's when we'll call EndGetAirports.