views:

1225

answers:

4

I was going through MSDN documentation on WebServices. Here and here, both these links talk about calling a webservice and wait for the response, which is also a general trend that I have seen while asynch implementation.

I don't understand "why do we need to wait for service call to return"? And, if we are waiting why don't make an synchronous call. What is the difference between an "asynch call followed by wait" and a "synchronous call"?

+1  A: 

Using asynchronous call can free up your application to do other things while waiting for the response. Since there is a fairly large amount of time (in computer cycles) waiting for a web server to respond, that time can be used for better things such as displaying a status update or doing some other work.

For example, if you had a program that performed a complicated calculation and a step of that calculation included using some reference data from a remote web service. By calling the web service asynchronously at the start of the calculation, continuing the parts of computation that can be performed locally, and then using the result of the web service call when it is available to complete the computation you can reduce the overall time of the calculation.

Since your application code is not blocked waiting for the web service to respond, you are able to utilize that wait time to the benefit of the user.

Another reason is scaling, particularly in web sites that make calls to other web services. By using asynchronous page methods (or tasks), IIS can scale your application more effectively by deferring your pages that are waiting on asynchronous web requests to whats known as an "IO thread", freeing up the main ASP.NET worker threads to serve more web pages.

Chris Patterson
Doesn't the call to result.AsyncWaitHandle.WaitOne() block the application though, defeating the point? That's what @noob2487 is getting at, I think.
Matt Hamilton
@Matt: That's correct.
noob.spt
You don't have to call it right away. You also don't have to block your thread. You can use Page.RegisterAsyncTask or you can use the BeginXXX/EndXXX syntax of a [WebMethod], letting ASP.NET do the waiting for you.
Chris Patterson
+3  A: 

To be useful, the asynchronous call needs to do its thing while you go do something else. There are two ways to do that:

  1. Provide a callback method for the asynchronous handle, so that it can notify you when it is completed, or

  2. Periodically check the asynchronous handle to see if its status has changed to "completed."

You wouldn't use a WaitHandle to do these two things. However, the WaitHandle class makes it possible for clients to make an asynchronous call and wait for:

  • a single XML Web service (WaitHandle.WaitOne),
  • the first of many XML Web services (WaitHandle.WaitAny), or
  • all of many XML Web services (WaitHandle.WaitAll)

to return results.

In other words, if you use WaitOne or WaitAny on an asynchronous web service that returns several results, you can obtain a single result from your web service call, and process it while you are waiting on the remaining results.

Robert Harvey
That's where I am confused. I understand the need for asynchronous calls, but then wait after making a call defeats the whole purpose.In fact, I believe the default way in which we do ASP.NET page asynch call also makes application wait till it gets a response back. I will try finding more info and post here.
noob.spt
@Noob2487, see my edit.
Robert Harvey
@Robert/@Brian: So you are suggesting making multiple calls at one go. How do I know server is ready to process calls simultaniously or whether it gonna choke the network or server? We are not using Message Queues here.
noob.spt
Then the callback or polling methods might work better for you than WaitOne or WaitAny.
Robert Harvey
Ok. I wonder if WSE provides any additional functionality for asynch processing.
noob.spt
The documentation for WSE 3.0 appears to suggest that it supports this kind of message queueing: http://msdn.microsoft.com/en-us/library/ms996942.aspx
Robert Harvey
+1  A: 

The first example you're linking to issues an async call and then immediately waits for the result. Other than forking off the job to another thread, there's little difference between this and a synchronous call as far as I can tell.

The other example, however, talks about doing multiple async calls at once. If this is the case, it makes sense to launch all calls and then wait because the calls may execute in parallel.

Brian Rasmussen
I see what you are saying about multiple async calls, but doesn't that still mean each individual caller has to wait?
Robert Harvey
You could fire up multiple calls from one thread and have that wait for these to return.
Brian Rasmussen
You could indeed fire up multiple and use WaitHandle.WaitAll() to make a single wait for all the requests to finish.
Chris Patterson
+2  A: 

One very practical use of asynchronous calls is stuff like this

If you want to update your UI while you're waiting for a 'server' to do something, you need to make an asynchronous call. If you make a synchronous call, your code will be stuck waiting, but if you make an asynchronous call you can update the UI or even let the user go do other stuff while you're waiting for the callback. This goes beyond UI, you may make an asynchronous call to start some non-critical task and continue on with your code and its possible you don't even register for a callback if the result is unimportant.

If you do NOTHING while waiting for the asyncronous call, then its less useful.

TJB
This makes sense. Cos if I make a synchronous call in this case, the application will TimeOut. So asynchronous call, will allow us to wait for the response while showing progress bar to user. If possible can you post some useful link with similar implementation (or I can google it also). Thanks.
noob.spt
It all depends on what development you're doing (win forms, web, silverlight?) here's one though just on pg 1 of googlehttp://msdn.microsoft.com/en-us/library/aa480520.aspx
TJB