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)
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.
// 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
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.