oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Calling Web Services Asynchronously

by Raj Makkapati

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:

  1. CallBacks
  2. WaitHandles

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]
public string MyWebMethod(string lang)
  return "You speak " + lang;

public void CreateXMLFile(string lang)
   // To set a 3 second thread sleep
   // Create an XMLDocument instance
   XmlDocument doc = new System.Xml.XmlDocument();

   // Load the Xml String into XmlDocument
   doc.LoadXml("<Root xmlns=\"http://CreateXMLFile\">
   // Save the document

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.

Real World Web Services

Related Reading

Real World Web Services
Integrating EBay, Google, Amazon, FedEx and more
By Will Iverson

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


// CallBack function
private void CallbackSampleMethod(IAsyncResult asyncResult)
   // Create an instance of the WebService
   localhost.MyAsyncWebService webServ = 
   // 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:

  • WebMethod parameters (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).

Pages: 1, 2, 3

Next Pagearrow