Proxies that are generated using the WSDL tool can be used to call Web services synchronously, where their thread is locked until the service returns the method call, or asynchronously, where after the call is made to the Web service, the client continues its operation until the Web service returns and calls a previously defined function as a callback. To do this in code, the AsyncCallback class and IAsyncResult interface are used (refer to Listing 2-1). Listing 2-1. AsyncCallback and I AsyncResult Interface
In this case the client, calling upon clicking Button1, defines an instance of the Web service proxy called myService. It generates an asynchronous callback called cb that defines its callback function as ServiceCallback. It then calls the BeginHelloWorld method, which has been generated by the proxy as an asynchronous caller to the HelloWorld method. When the service completes, it does a callback to ServiceCallback, which pulls the state of the call from the IAsyncResult interface and gets from it the returned value from the Web service. |