Coffee, smoke and Techilicious Burps at 3.47 AM Headline Animator

Thursday, April 19, 2007

What would you have to do to implement an asynchronous Web service method

  1. Split a synchronous Web service method into two methods, each with the same base name, one with that name starting with Begin and the other End.

  2. The parameter list for the Begin method contains all the in and by reference parameters for the method's functionality plus two additional parameters.

    • By reference parameters are listed as in parameters.

    • The second from the last parameter must be an AsyncCallback. The AsyncCallback parameter allows a client to supply a delegate, which is invoked when the method completes. When an asynchronous Web service method calls another asynchronous method, this parameter can be passed into the second from last parameter for that method.

    • The last parameter is an Object. The Object parameter allows a caller to supply state information to the method. When an asynchronous Web service method calls another asynchronous method, this parameter can be passed into the last parameter for that method.

    • The return value must be of type IAsyncResult.

  3. The parameter list for the End method consists of an IAsyncResult followed by any out and by reference parameters specific to the method's functionality.

    • The return value is the same type as the return value of a synchronous Web service method.

    • By reference parameters are listed as out parameters.

Example

using System;
using System.Web.Services;

[WebService(Namespace="http://www.contoso.com/")]
public class MyService : WebService
{
public RemoteService remoteService;
public MyService()
{
// Create a new instance of proxy class for
// the Web service to be called.
remoteService = new RemoteService();
}
// Define the Begin method.
[WebMethod]
public IAsyncResult BeginGetAuthorRoyalties(String Author,
AsyncCallback callback, object asyncState)
{
// Begin asynchronous communictation with a different XML Web
// service.
return remoteService.BeginReturnedStronglyTypedDS(Author,
callback,asyncState);
}
// Define the End method.
[WebMethod]
public AuthorRoyalties EndGetAuthorRoyalties(IAsyncResult
asyncResult)
{
// Return the asynchronous result from the other Web service.
return remoteService.EndReturnedStronglyTypedDS(asyncResult);
}
}

No comments: