How to: Call a Web Service Asynchronously (Visual Basic)

This example attaches a handler to a Web service's asynchronous handler event, so that it can retrieve the result of an asynchronous method call. This example used the DemoTemperatureService Web service at

When you reference a Web service in your project in the Visual Studio Integrated Development Environment (IDE), it is added to the My.WebServices object, and the IDE generates a client proxy class to access a specified Web service

The proxy class allows you to call the Web service methods synchronously, where your application waits for the function to complete. In addition, the proxy creates additional members to help call the method asynchronously. For each Web service function, NameOfWebServiceFunction, the proxy creates a NameOfWebServiceFunctionAsync subroutine, a NameOfWebServiceFunctionCompleted event, and a NameOfWebServiceFunctionCompletedEventArgs class. This example demonstrates how to use the asynchronous members to access the getTemp function of the DemoTemperatureService Web service.


This code does not work in Web applications, because ASP.NET does not support the My.WebServices object.

To call a Web service asynchronously

  1. Reference the DemoTemperatureService Web service at The address is  
  2. Add an event handler for the getTempCompleted event:

    Private Sub getTempCompletedHandler(ByVal sender As Object,   
        ByVal e As net.xmethods.www.getTempCompletedEventArgs)  
        MsgBox("Temperature: " & e.Result)  
    End Sub  


    You cannot use the Handles statement to associate an event handler with the My.WebServices object's events.

  3. Add a field to track if the event handler has been added to the getTempCompleted event:

    Private handlerAttached As Boolean = False  
  4. Add a method to add the event handler to the getTempCompleted event, if necessary, and to call the getTempAsynch method:

    Sub CallGetTempAsync(ByVal zipCode As Integer)  
        If Not handlerAttached Then  
            AddHandler My.WebServices.  
                AddressOf Me.TS_getTempCompleted  
            handlerAttached = True  
        End If  
    End Sub  

    To call the getTemp Web method asynchronously, call the CallGetTempAsync method. When the Web method finishes, its return value is passed to the getTempCompletedHandler event handler.

See Also

Accessing Application Web Services
My.WebServices Object