Implementing an asynchronous XML Web service method follows the .NET Framework asynchronous design pattern.
So if we had a Web method whose synchronous declaration looked like this:
Then an asynchronous declaration would look like this:
We split the synchronous method into two methods; each with the same base name - one with that name starting with Begin and the other End. The BeginLengthyProcedure function returns an IAsyncResult interface and takes as its last two input parameters an AsyncCallback, and an object respectively. The EndLengthyProcedure function takes as its only parameter an IAsyncResult interface. Both must be flagged with the WebMethod attribute. In the BeginLengthyProcedure function, I made an asynchronous Web service call. The call takes the same cb, a parameter for BeginLengthyProcedure, as its parameter. This will cause that the EndLengthyProcedure function is invoked when the DelayedResponse call completes. The EndLengthyProcedure function will harvest the result of the DelayedResponse call and return it.
The following code example demonstrates how to chain asynchronous calls when a Web service method makes more than one asynchronous call and the calls must execute sequentially.
We have Web services: Service1 with a Web method DelayedResponse and Service 2 with a Web method DelayedResponse2. The BeginLengthyProcedure method makes an asynchronous call to Service 1 and sets up an intermediate callback named ChainCallback to receive the results. That intermediate callback then asynchronously calls to Service 2 and The EndLengthyProcedure function will harvest the result of the DelayedResponse2 call and return it.