Interoperability Using Asynchronous Web Services


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

private void Button1_Click(object sender, System.EventArgs e) { localhost.Service1 myService = new localhost.Service1(); AsyncCallback cb = new AsyncCallback(WebServiceCallback); myService.BeginHelloWorld(cb,myService); } public void WebServiceCallback(IAsyncResult ar) { localhost.Service1 myService =            (localhost.Service1)ar.AsyncState; string strTest = myService.EndHelloWorld(ar); } 

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.




Java EE and. Net Interoperability(c) Integration Strategies, Patterns, and Best Practices
Java EE and .NET Interoperability: Integration Strategies, Patterns, and Best Practices
ISBN: 0131472232
EAN: 2147483647
Year: N/A
Pages: 170

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net