ASP.NET Async Pages vs Async WCF Service Operation

In Dmitry’s blog, he mentioned about how to write ASP.NET async pages with ASP.NET 2.0. Basically, the server page can handle the request asynchronously without blocking the request thread and the server thread can be returned to the thread pool to handle other requests.

WCF is quite flexible in supporting asynchronous programming. Actually the client and the service can use any pattern (sync or asnc) in their own way to perform the same operation. For example, you can use the following service contract with an async operation in it:

[ServiceContract(Namespace = "https://tempuri.org", Name = " HelloService")]

interface IHelloService

{

    [OperationContract(IsOneWay = false, AsyncPattern = true)]

    IAsyncResult BeginHello(string text, AsyncCallback callback, object state);

    string EndHello(IAsyncResult result);

}

This allows you to implement the service operation in the same way as ASP.NET async pages.

On the client side, you can access the service either from an async call or a sync call. For example, you can have a sync client to access the above async service operation with the following client contract:

[ServiceContract(Namespace = "https://tempuri.org", Name = "HelloService")]

interface IHelloService

{

    [OperationContract(IsOneWay = false)]

    string Hello(string text);

}

The sample code is attached for download.

 

AsyncHello052006.zip