Review of Making Web Service Calls
page 3 of 17
by Wenfeng Gao
Feedback
Average Rating: 
Views (Total / Last 10 Days): 78456/ 138
Article Contents:

Making an Asynchronous Web Service Call

As noted earlier, the generated proxy class provides a pair of methods prefixed with Begin and End to call the Web service asynchronously.

 

Before retrieving the results of an Asynchronous Web service call, the application must first determine that the call has completed. An application can choose to be notified of completion, use WaitHandles or can poll to determine if the Web service call has completed.

 

Notification

 

In most cases, notification is the best choice as it allows the application to initiate the call to the Web service and then proceed without any other special handling.

 

 

The application passes an AsyncCallback delegate to the BeginDelayedResponse method. The delegate will automatically be called when the Web service call completes.

 

Note that the call to BeginDelayedResponse does not directly call the Web service but instead queues the actual Web service call then returns immediately. No information from the Web service itself is available when the call returns.

 

Using notifications can be very efficient if you are making a lot of simultaneous Web service calls.

 

The callback function will run on a thread pool thread. So you should use Control.Invoke to send the response to the UI thread. Using notifications is the only option to allow you to release the thread you are currently executing in.

 

Polling

 

In some rare occasions, an application may wish to continue processing while periodically checking on the Web service call.

 

 

Using IAsyncResult.IsCompleted, the application can determine if the Web service call has completed. If EndDelayedResponse is called before the request has completed, it will simply block until the request does complete.

 

One of the problems you need to watch out for when using polling to determine if your call has completed is that you can end up using a lot of your machine's CPU cycles if you are not careful.

 

Using WaitHandles

 

WaitHandles are convenient for scenarios where you need to make asynchronous calls, but you do not want to release the thread you are currently executing in. With WaitHandles you can do some processing after your Web service call has been made and then block until the Web service call has completed.

 

 

When the Web service call completes, the AsyncWaitHandle is signaled. Using the WaitOne method on the AsyncWaitHandle will cause the calling thread (normally the main application thread) to block until the Web service completes.

 

There are also static methods called WaitAll and WaitAny in the WaitHandle class. These two static methods take arrays of WaitHandles as parameters, and either return when all the calls have completed, or as soon as any of the calls have completed, depending on which function you call. WaitAll allows multiple Web service calls to execute at the same time.

 

The WaitOne, WaitAll, and WaitAny methods all have the option of taking a timeout as a parameter. This allows you to control just how long you are willing to wait for a Web service to complete.

Many times, applications will combine WaitHandles with one of the other asynchronous approaches. I’ll show you an example later.

 

6. A synchronous Web service call with an asynchronous delegate vs. an asynchronous Web service call

 

Asynchronous Web service calls are more efficient, especially for an infrequent call or multiple calls. Two approaches are almost the same for a frequent call.

 

The best way to make Web service calls from Windows Forms clients is to make asynchronous Web service calls or combine asynchronous Web service calls with an asynchronous delegate.

 

7. Asynchronous Web service calls with asynchronous delegates

 

Many times, applications will combine asynchronous Web service calls with an asynchronous delegate. For example, you want to generate a report. Report data are from a remote database and a Web service. Both calls might be lengthy calls.

 

 

Using an asynchronous delegate sends the task from the UI thread to a thread pool thread. Using an asynchronous Web service call allows to retrieve data from the database and the Web service simultaneously.

 


View Entire Article

User Comments

Title: Code example   
Name: Sean Anderson
Date: 2009-05-26 8:02:17 AM
Comment:
Likewise, I would be very interested in seeing a working example of this, as I have read the article here (and the one on MSDN) both of which give a very good overview of the approach, but without a full example I am rapidly getting stuck.
Title: Great articles but...   
Name: Pascal
Date: 2009-05-21 7:38:29 AM
Comment:
Same comments, great articles but would be nice to have a sample code, not that easy to try when you don't know where you going.
Title: req sample code downloadable..   
Name: Karthik
Date: 2006-01-02 1:13:44 AM
Comment:
Hi,
the article was a nice one.
it would be more good if sample code(downloadable) for asynchronous webservices.
can that be done?

Regards,
Karthik






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-24 1:42:31 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search