WCF and the AsyncPattern property (part 1)

The OperationContractAttribute is used, in WCF, to define operations on a service contract. This attribute contains the AsyncPattern property that can be used to implement or use services asynchronously.

AsyncPattern at the service side

Consider the following excerpt

   1: [ServiceContract]
   2: interface IService {
   3:     [OperationContract]
   4:     ReplyData Operation1(RequestData rd);
   5:  
   6:     [OperationContract(AsyncPattern = true)]
   7:     IAsyncResult BeginOperation2(RequestData rd, AsyncCallback ac, object obj);
   8:     ReplyData EndOperation2(IAsyncResult ar);
   9: }
  • The first [OperationContract], which has AsyncPattern set to false (the default), defines one request-response operation called ‘Operation1’. This operation is implemented synchronously by one method: Operation1. Synchronously means that the WCF dispatcher will call the Operation1 method, passing the RequestData. This call will block during the execution of the operation. Finally, the method returns the ReplyData that will be used in the response sent to the client.

 

  • The second [OperationContract], which has AsyncPattern set to true, also defines one request-response operation called ‘Operation2’. However, this operation is implemented asynchronously by two methods: BeginOperation2 and EndOperation2. Asynchronously (see Asynchronous Programming Overview) means that:
    • The WCF dispatcher will call the BeginOperation2 method, passing the RequestData, an AsyncCallback and some state information.
    • This method should start the execution of the operation and return an IAsyncResult immediately.
    • When the execution of the operation is completed, then the AsyncCallback should be called.
    • This AsyncCallback, implemented by the WCF runtime, will then call EndOperation2 to collect the ReplyData that will be used in the response sent to the client.

Note that, when using AsyncPattern set to true, there is no longer a 1-to-1 relation between the service contract methods and the service operations. Namely, the BeginOperation2 and EndOperation2 methods are both associated to a single operation named ‘Operation2’.

Also, from the outside of the service, both operations (‘Operation1’ and ‘Operation2’) are seen as request-response operations. This is visible in the generated WSDL description

   1: <wsdl:portType name="IService">
   2:  
   3:   <wsdl:operation name="Operation1">
   4:     <wsdl:input wsaw:Action="..." message="..." /> 
   5:     <wsdl:output wsaw:Action="..." message="..." /> 
   6:   </wsdl:operation>
   7:  
   8:   <wsdl:operation name="Operation2">
   9:     <wsdl:input wsaw:Action="..." message="..." /> 
  10:     <wsdl:output wsaw:Action="..." message="..." /> 
  11:   </wsdl:operation>
  12:  
  13: </wsdl:portType>

So, concluding, the AsyncPattern is an implementation detail of the service: it defines the communication pattern between the WCF runtime and the service implementation but not the communication pattern between the service client and the service.

Advertisements

4 thoughts on “WCF and the AsyncPattern property (part 1)

  1. Pingback: WCF and the AsyncPattern property (part 2) « Pedro Félix’s shared memory

  2. Pingback: WCF and the AsyncPattern property (part 3) « Pedro Félix’s shared memory

  3. Val

    Thanks, for the post!

    Can you please give some good resons and circumstances when one should implement a service asynchronously? Usually async operation is associated with doing the job on another thread – it seems to me that it is only a waste of a thread.

    Thanks
    Val

    Reply
    1. pedrofelix Post author

      Hi Val,

      1) The typical scenario for implementing a service asynchronously is when the service implementation performs one or more asynchronous calls, such as database calls or external service calls. Consider for instance a middle-tier service that uses a back-end service or data store that has a significant latency.

      2) I don’t agree that “Usually async operation is associated with doing the job on another thread”. An async operation is one that is performed without requiring the blocking of the calling thread. This does not necessarily means that the work is done on another thread. For instance, when asynchronously calling a WCF service, no extra thread is blocked.

      3) Consider, for instance, a middle-tier service that uses slow (high latency) back-end services. This could mean that, one a given moment of time, there are are several service operations waiting for the back-end service responses. Using a synchronous implementation, this implies one blocked thread for each waiting operation, which can exhaust the server threads. Using asynchronous implementation, each service operation is waiting without requiring a waiting thread. (see the diagram at https://pfelix.files.wordpress.com/2008/06/asyncpattern1.png)

      Did this help?
      Regards
      Pedro

      Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s