Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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 http://www.xmethods.net.
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.
Note
This code does not work in Web applications, because ASP.NET does not support the My.WebServices object.
Call a Web service asynchronously
Reference the DemoTemperatureService Web service at
http://www.xmethods.net. The address ishttp://www.xmethods.net/sd/2001/DemoTemperatureService.wsdlAdd an event handler for the
getTempCompletedevent:Private Sub getTempCompletedHandler(ByVal sender As Object, ByVal e As net.xmethods.www.getTempCompletedEventArgs) MsgBox("Temperature: " & e.Result) End SubNote
You cannot use the
Handlesstatement to associate an event handler with theMy.WebServicesobject's events.Add a field to track if the event handler has been added to the
getTempCompletedevent:Private handlerAttached As Boolean = FalseAdd a method to add the event handler to the
getTempCompletedevent, if necessary, and to call thegetTempAsyncmethod:Sub CallGetTempAsync(ByVal zipCode As Integer) If Not handlerAttached Then AddHandler My.WebServices. TemperatureService.getTempCompleted, AddressOf Me.TS_getTempCompleted handlerAttached = True End If My.WebServices.TemperatureService.getTempAsync(zipCode) End SubTo call the
getTempWeb method asynchronously, call theCallGetTempAsyncmethod. When the Web method finishes, its return value is passed to thegetTempCompletedHandlerevent handler.