Asynchronous operations in WCF

Posted on March 7, 2011 by

0


WCF contracts can contain both synchronous and a-synchronous operations.

A-synchronous operations must follow the following rules:

  • They must follow the a-synchronous pattern: (see http://msdn.microsoft.com/en-us/library/aa719599(v=VS.71).aspx)
    • The Begin method takes the method parameters, a callback object, and a state object, and returns a System.IAsyncResult
    • The matching End method takes a System.IAsyncResult and returns the return value of the method
  • The BeginXXX() must be marked with the WCF OperationContract attribute, with the AsyncPattern parameter set to true.
    [ServiceContract]
    public interface ICalculator
    {
        [OperationContract]
        int subtract(int x, int y);

        [OperationContract(AsyncPattern = true)]
        IAsyncResult BeginAdd(int x, int y,AsyncCallback cb, Object state);

        int EndAdd(IAsyncResult ar);
    }

Implementing asynchronous services is required when your server side performs heavy operations which might block the call for a long duration, such as I/O work, approaching databases etc.

In all other cases, implement the service synchronously (which will cause the operation to block during the execution of the operation)

It is important to mention that there is no dependency between the way the client is implemented and the way the server is. Meaning, the client can implement the contract synchronously while it is implemented asynchronously on the server side and vice versa.

The reason, there is no dependency is related to the dispatcher on the server side and the way it attaches messages to operations. The default behavior selects the destination operation according to the value of the SOAP action header of the message, and synchronous and asynchronous operations have the same action header. (For example: both Add() and BeginAdd()/EndAdd() will have the following action header – http://tempuri.org/ICalculator/Add )

When generating the WCF client code from the metadata documents using the Svcutil.exe (http://msdn.microsoft.com/en-us/library/aa347733.aspx), adding the

/async flag causes the generation of both synchronous and asynchronous method signatures (leaving the client to decide which signature it wants to use).

So, what actually happens in WCF when invoking asynchronous operations?

WCF performs two tasks:

1. It validate that the operation with the ‘AsyncPattern = true’ follows the a-sync pattern (It validates that there are BeginXXX() and a corresponding EndXXX() with the needed parameters and return values). If the operations don’t follow the a-sync pattern an InvalidOperationException is thrown when the contract description is created.

2. WCF executes the operation synchronously on a new thread from the thread pool (which will block until the operation ends), but the calling thread returns immediately.

useful links:

http://msdn.microsoft.com/en-us/library/ms730059.aspx

http://msdn.microsoft.com/en-us/library/ms734701.aspx

Advertisements
Posted in: General