Geeks With Blogs

News My Blog has been MOVED to
Michael Freidgeim's OLD Blog My Blog has been MOVED to
I need to asynchronously call a few web services at the same time.
VS 2005/2008 Web services proxy  generator creates several proxy class methods , including
synchronous MyWebMethod(parameters) and asynchronous pair BeginMyWebMethod(parameters) and EndMyWebMethod(parameters)
The way to use Begin/End methods is described in article "Calling Web Services Asynchronously
Even if I am going to use asynchronous method in the real application, for unit testing(including TestHarness ) it is much easier to start with synchronous version. 
Correct population of all  input parameters and reading response can take some time for coding/debugging(if the web service method is not trivial).
Usually this code wrapper implemented as a separate function (e.g. CallMyWebMethod) in your client application.
If you want to use asynchronous pair , your will need to duplicate the wrapper functions as BeginCallMyWebMethod , that will filled request data and EndCallMyWebMethod function, to read and interprete response.
I found that it is simpler to use ability to call Synchronous Methods Asynchronously (using delegate invoke) directly in your code rater than use pre-generated Begin/End pair.
// call the service.

MyClass.ExecuteDelegate requestDelegate =new MyClass.ExecuteDelegate(myRequest.CallMyWebMethod);

MyAsyncState asyncState =new MyAsyncState (myData);

IAsyncResult asyncResult= requestDelegate.BeginInvoke(null, asyncState);

 //and when call will be completed

 AsyncResult asyncResult=(AsyncResult)ar;

MyClass.ExecuteDelegate requestDelegate = (MyClass.ExecuteDelegate)asyncResult.AsyncDelegate;

 MyResponseClass myResponse =(MyResponseClass )requestDelegate.EndInvoke(asyncResult); 

In this case the actual custom  application code of CallMyWebMethod function stays unmodified for both sync and async execution.

Similar approach was described in my old post Asynchronous long-running tasks in ASP.NET application
Posted on Friday, July 18, 2008 9:23 PM .Net Framework , Web Services/WCF | Back to top

Comments on this post: Call Synchronous Web Services Methods Asynchronously

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © Michael Freidgeim | Powered by: