Calling Web Services Asynchronously
by Raj Makkapati08/01/2005
Making synchronous calls to web services can be problematic on occasion, because they have the potential to cause considerable delay. The reason for this is the manner in which synchronous calls work. The application blocks the client until the web service call returns. To overcome the necessity of having to wait for the web service response, we can call web services asynchronously. The asynchronous call mechanism does not need any additional functionality for the web service to handle it competently. The decision on whether to call synchronously or asynchronously belongs to the client.
Currently there are two methods of performing asynchronous calls:
CallBacksWaitHandles
Here are a couple of web service web methods that we will be using in the demo.
Two web methods, MyWebMethod and CreateXMLFile, are
declared in the web service. MyWebMethod method takes a string
parameter and concatenates "You speak " to it and returns the result. The CreateXMLFile
method takes a string parameter and creates an XML file in the C://temp directory:
// [C# Code]
[WebMethod()]
public string MyWebMethod(string lang)
{
System.Threading.Thread.Sleep(3000);
return "You speak " + lang;
}
[SoapDocumentMethod(OneWay=true)]
[WebMethod()]
public void CreateXMLFile(string lang)
{
// To set a 3 second thread sleep
System.Threading.Thread.Sleep(3000);
// Create an XMLDocument instance
XmlDocument doc = new System.Xml.XmlDocument();
// Load the Xml String into XmlDocument
doc.LoadXml("<Root xmlns=\"http://CreateXMLFile\">
<Resultxmlns=\"\">"+lang+"</Result></Root>");
// Save the document
doc.Save("C:\\temp\\MyFile1.xml");
}
Before we jump off to writing asynchronous calls, let us take a look at how the synchronous call is made so that we can better understand the asynchronous call mechanism.
|
Related Reading Real World Web Services |
Synchronous Call
// Create an instance of the WebService
localhost.MyAsyncWebService webServ =
new localhost.MyAsyncWebService();
// Return the result string
strResult = webServ.MyWebMethod("English");
The synchronous call mechanism is pretty straightforward; all we would need to do would be to declare an instance of the web service. As the web service is running locally, we would need to append the string "localhost" to the web service name. Once the web service is declared, web methods can be called as demonstrated above. Now let us start writing asynchronous calls.
Asynchronous Call using an AsyncCallback
In this approach, we create a delegate which can be invoked during runtime when the results from the web service are returned. Here's how it works.
// Button Click event
private void AsyncCallUsingCallBackBtn_Click
(object sender, System.EventArgs e)
{
string mLangInput = "English";
// Create an instance of the WebService
localhost.MyAsyncWebService webServ =
new localhost.MyAsyncWebService();
// Create a delegate to handle the callback
AsyncCallback asyncCall =
new AsyncCallback(CallbackSampleMethod);
// Make an Asynchronous Call by calling
// the Begin method of the proxy class
webServ.BeginMyWebMethod(this.mLangInput, asyncCall, webServ);
// Do some process while the web
// service is processing the request
System.Threading.Thread.Sleep(10000);
}
// CallBack function
private void CallbackSampleMethod(IAsyncResult asyncResult)
{
// Create an instance of the WebService
localhost.MyAsyncWebService webServ =
(localhost.MyAsyncWebService)asyncResult.AsyncState;
// Get the Result of the WebMethod by calling
// the end method of the proxy class
mLangResult = webServ.EndMyWebMethod(asyncResult);
// Display the results in a label
Label1.Text = this.mLangResult;
}
In the Button Click event, the proxy's Begin<WebServiceMethod>(BeginMyWebMethod)
web service is called by passing in the following parameters:
WebMethodparameters (this.mLangInput)AsyncCallback(asyncCall)- An instance of the web service(
webServ)
The CallBack function should have an instance of the IAsyncResult instance as a parameter
(see the above code). After the web service returns the results, then the Callback
function is activated (CallbackSampleMethod). Results of the web
service are obtained by calling the proxy's End<WebServiceMethod> (EndMyWebMethod).


