Asynchronous Programming in Web Services


You can also employ an asynchronous call for a method on a Web service along with a synchronous call. What's special about asynchronous programming? The unique thing in asynchronous programming is that after sending the request to the Web service, the client need not wait for the request to be completed.

If a large amount of data is to be returned from a Web service, you can use the asynchronous method. This will greatly improve the application performance. The client can do any further useful execution until the result is returned from the Web service. It is imperative to note that a Web service does not have to be exclusively written to handle asynchronous requests to be called asynchronously.

When the proxy class is created using either Wsdl.exe or Visual Studio .NET for synchronous call, the methods needed for calling the Web service method asynchronously are also created automatically.

Two Asynchronous Methods (Begin and End)

For each synchronous method, there is a Begin and an End asynchronous method. For example, in the proxy Concatenate.cs file, for the synchronous Concatenate( ) method, there are two asynchronous methods: BeginConcatenate( ) and EndConcatenate( ) , as shown next .

[View full width]
 
[View full width]
public string Concatenate(string s1, string s2) { object[] results = this.Invoke("Concatenate", new object[] { s1, s2}); return ((string)(results[0])); } public System.IAsyncResult BeginConcatenate(string s1, string s2, System.AsyncCallback graphics/ccc.gif callback, object asyncState) { return this.BeginInvoke("Concatenate", new object[] {s1,s2}, callback, asyncState); } public string EndConcatenate(System.IAsyncResult asyncResult) { object[] results = this.EndInvoke(asyncResult); return ((string)(results[0])); }

The Begin method is called by a client to start the process of the Web service method (request). The End method is called by the client to get the results of the processing done by the Web service method call (response). After calling the Begin method to start the process, when do we have to call the End method? How will we know the asynchronous Web service call has completed?

There are four ways to determine when the asynchronous Web service call has completed.

  1. A callback delegate is passed along with the Begin method, and that callback function is called by the proxy when the Web method has completed processing.

  2. A client waits for the method to complete using one of the methods of the WaitHandle class.

  3. The value of IAsyncResult.IsCompleted is polled to check the completion of the process. When this property returns true, the Web service response is available.

  4. The End method is called directly.

Let us discuss the first two methods in detail.



.NET Security and Cryptography
.NET Security and Cryptography
ISBN: 013100851X
EAN: 2147483647
Year: 2003
Pages: 126

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