This article has been
excerpted from book "A Programmer's Guide to ADO.NET in C#".
There are two ways to access web services from a client application synchronously and asynchronously. A synchronous Web service is like the one we
just executed in our client application. When a client is accessing a web
service synchronously, a client has to wait until it gets a response from the
web service. As we saw in our client application in the previous section, we're
accessing OrderRetrievalService synchronously. What if a web method takes a long
time in response? For instance, what if our InsertOrderFromNode method takes 10
minute in response and client has many other things to process? In the present
case, the client has to wait for 10 minute and can't execute any thing else
until getting a response from web service. In other words, a client's main
thread is busy with the Web service if the client is accessing a Web service
synchronously. Executing a web service asynchronously allows client application
to execute more than one thread simultaneously. In other words, by calling
accessing web services asynchronously, a client can continue to use the calling
thread while waiting for the XML web service to respond. To access Web services
asynchronously doesn't require any special configuration.
When adding a Web reference to a client application, VS.NET generates a proxy
class capable of calling an XML Web service both synchronously and
asynchronously. You can see the proxy reference of OrderRetrievalService by
expanding the mcb node in the Solution Explorer; the reference filename is
Reference.cs
To view the contents of the proxy file, right-click and select View Code. This
file stores information related to the web service and its methods. It also
contains the URL of the web service. You can find the following code in
Reference.cs as OrderRetrievalService.
///
<remarks/>
public Service1()
{
this.Url =
"http://mcb/
OrderRetrievalService/Service1.asmx";
}
Other information this file stores is asynchronous method for every web method
published by a web service. For each web method, there are two contains a
corresponding Begin and End method. Therefore, for web method
InsertOrderFromNode, asynchronous calls are BeginInsertOrderFromNode and
EndInsertOrderFromNode. You can find the following methods in Reference.cs file:
///<remarks/>
public System.IAsyncResult
BeginInsertOrderFromNode
(System.Xml.XmlNode aNode,
System.AsyncCallback callback,
object asyncState)
{
return this.BeginInvoke("InsertOrderFromNode",
new object[] {
aNode}, callback, asyncState);
}
///
<remarks/>
public int
EndInsertOrderFromNode(System.IAsyncResult
asyncResult)
{
object[] results =
this.EndInvoke(asyncResult);
return ((int)(results[0]));
}
Asynchronous web service execution requires a two-step process. First, you tell
the Web service to begin execution by calling the Begin method. The second step,
calling the End method, completes the web service call and returns the response.
To call a web service asynchronously, a client thread can use a WaitHandle. When
you get to a place in your client code when you are required to wait for the
service to end, you call a WaitHandle there. Listing 8-9 is an example of how to
use the WaitHandle. You can also assign a WaitHandle a timeout period in case
the web service takes to long.


Join the conversation! Your thoughts help the community grow.