How to: Implement an Event-Driven Asynchronous Web Service Client Using ASP.NET 2.0

How to: Implement an Event-Driven Asynchronous Web Service Client Using ASP.NET 2.0

This topic is specific to a legacy technology. XML Web services and XML Web service clients should now be created using Windows Communication Foundation .

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

  1. Create an XML Web service with a synchronous Web method that performs some time-consuming behavior that is best done asynchronously.

    [WebMethod]
    public string HelloWorld() {
      Thread.Sleep(5000);
      return "Hello World";
    }
    

    <WebMethod()> _
    Public Function HelloWorld() As String 
      Thread.Sleep(5000)
    ..Return "Hello World"
    End Function
    
  2. In the client ASP.NET application, add the Async attribute to your @ Page directive and set it to true, and use the @ Import directive to import the System.Threading namespace.

    <%@ Page Language="C#" Debug="true" Async="true" %>
    <%@ Import Namespace="System.Threading" %>
    

    <%@ Page Language="VB" Debug="true" Async="true" %>
    <%@ Import Namespace="System.Threading" %>
    
  3. 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.)

  4. Build the Web service client application normally by creating a new object using the service class name plus the string WaitService and assign the Web service URL to the Url property. For example, if the service class name is HelloWorld, then your client creates a HelloWorldWaitService object.

    HelloWorldWaitService service = new HelloWorldWaitService();
    service.Url = "http://localhost/QuickStartv20/webservices/Samples/RADAsync/cs/Server/HelloWorldWaitService.asmx";
    

    Dim service As New HelloWorldWaitService()
    service.Url = "http://localhost/QuickStartv20/webservices/Samples/RADAsync/vb/Server/HelloWorldWaitService.asmx"
    
  5. 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 HelloWorldCompleted method that is called when the Web service method returns.

    //Add our callback function to the event handler. 
    service.HelloWorldCompleted += this.HelloWorldCompleted; 
    

    'Add our callback function to the event handler
    AddHandler service.HelloWorldCompleted, AddressOf Me.HelloWorldCompleted
    
  6. 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.

    service.HelloWorldAsync("second call");
    

    service.HelloWorldAsync("second call")
    

See Also

Community Additions

ADD
Show:
© 2016 Microsoft