How to: Implement an Event-Driven Asynchronous Web Service Client Using ASP.NET 2.0
In version 2.0 of the .NET Framework, the proxy code generated by Web Services Description Language Tool (Wsdl.exe) supports the new event-driven asynchronous programming model. By combining the event-driven asynchronous programming model and the automatic proxy generation of ASP.NET 2.0 Web clients, you can rapidly build high-performance Web service-based Web applications.
Multithreaded Programming with the Event-based Asynchronous Pattern introduces a new asynchronous programming model that uses events to handle callbacks, making it easier to build multithreaded applications without having to implement complex multithreaded code yourself. For an overview of the new event-driven asynchronous model, see Event-based Asynchronous Pattern Overview. For details about client implementations using the new model, see How to: Implement a Client of the Event-based Asynchronous Pattern.
Web service clients that are built using ASP.NET application in version 2.0 of the .NET Framework can take advantage of the new App_WebReferences subdirectory, which can dynamically compile a WSDL file into proxy code when the client ASP.NET application calls an XML Web service that supports the WSDL contract.
For the complete sample, see the RADAsync quickstart at ASP.NET Web Services QuickStarts.
Implementing an Event-Driven Web Service Client
Create an XML Web service with a synchronous Web method that performs some time-consuming behavior that is best done asynchronously.
<%@ Page Language="C#" Debug="true" Async="true" %> <%@ Import Namespace="System.Threading" %>
To use automatic proxy generation, generate a WSDL file (using the Web Services Description Language Tool (Wsdl.exe)) and place the file in the client App_WebReferences subdirectory. (For details, see ASP.NET Web Site Layout.)
Build the Web service client application normally by creating a new object using the service class name plus the string
WaitServiceand assign the Web service URL to the Url property. For example, if the service class name is
HelloWorld, then your client creates a
HelloWorldWaitService service = new HelloWorldWaitService(); service.Url = "http://localhost/QuickStartv20/webservices/Samples/RADAsync/cs/Server/HelloWorldWaitService.asmx";
In the client application code, assign an event handler to the Completed event of your proxy. In the following code example, the client ASP.NET page has a
HelloWorldCompletedmethod that is called when the Web service method returns.
//Add our callback function to the event handler. service.HelloWorldCompleted += this.HelloWorldCompleted;
In the client application code, call the Async method on the proxy. (This method is the same name as the Web method but with "Async" appended to it. For details, see How to: Implement a Client of the Event-based Asynchronous Pattern.) This method call appears as a synchronous call in the client ASP.NET page, but it returns immediately. The client ASP.NET page is not returned to the browser until the asynchronous call completes, the proxy's Completed event is raised, and the handler method has executed.