How to: Access a Web Service Asynchronously in Managed Code

 Windows Communication Foundation Services and ADO.NET Data Services

Every Web method on a Web service proxy class has an asynchronous counterpart. The proxy class automatically generates asynchronous methods and a corresponding event for every Web method. When the asynchronous method is called, it executes on another thread and raises its corresponding event when it returns. You can execute code when an asynchronous method returns by creating a handler for its corresponding event.

To call a Web method asynchronously with Visual Basic

  1. Declare an instance of the Web service proxy class using the WithEvents keyword, as shown below:

    Dim WithEvents myWebService As New Service1
    

    Note

    Your project must contain a Web reference to the Web service.

  2. In the Code Editor, use the Handles keyword to create an event handler for the MethodCompleted event that corresponds to the method you want to call. For example, if you were calling a method called HelloWorld asynchronously, you would create a method similar to the following:

    Private Sub HelloWorldComplete(ByVal sender As Object, _
       ByVal completed As localhost.HellowWorldCompletedEventArgs) _
       Handles myWebService.HelloWorldCompleted
    ' Insert code to implement the method here
    End Sub
    

    Note that the method that handles the MethodCompleted event must match the signature of the event. This usually requires an Object argument to represent the sender, and an instance of the method's EventArgs, which resides in the same namespace as the Web service proxy class. You can also use the Code Editor to auto-create event handlers for you. For more information, see How to: Create Event Handlers in the Visual Basic Code Editor.

  3. Call the Web method using the MethodAsync form of the method. For example, if you were calling a Web method called HelloWorld asynchronously, it would look as follows:

    HelloWorldAsync
    

    Note that the return value of the method is available in the Result property of the EventArgs.

To call a Web method asynchronously with C#

  1. Declare an instance of the Web service proxy class, as shown below:

    private localhost.Service1 myWebService = new localhost.Service1
    ();
    

    Note

    Your project must contain a Web reference to the Web service.

  2. In the Code Editor, add an event handler for the MethodCompleted event that corresponds to the method you want to call. For example, if you were calling a method called HelloWorld asynchronously, you would create a method similar to the following:

    private void HelloWorldCompleted(Object sender, 
        localhost.HelloWorldCompletedEventArgs Completed) 
        {
            // Insert code to implement the method here
        }
    

    Note that the method that handles the MethodCompleted event must match the signature of the event. This usually requires an Object argument to represent the sender, and an instance of the method's EventArgs, which resides in the same namespace as the Web service proxy class. You can also use the Code Editor to auto-create event handlers for you. For more information, see How to: Subscribe to and Unsubscribe from Events (C# Programming Guide).

  3. In the constructor for the class, add the MethodCompleted event handler to the list of handlers for that event, as shown below:

    private void Form1_Load(object sender, EventArgs e)
    {
        myWebService.HelloWorldCompleted += new 
        localhost.HelloWorldCompletedEventHandler(HelloWorldCompleted);
    }
    
  4. Call the Web method using the MethodAsync form of the method. For example, if you were calling a Web method called HelloWorld asynchronously, it would look as follows:

    HelloWorldAsync();
    

    Note that the return value of the method is available in the Result property of the EventArgs.

See Also

Other Resources

Accessing Web Services in Managed Code

Communicating with XML Web Services Asynchronously