WCF Concurrency

Introduction

 
Concurrency is the control of multiple threads active in an InstanceContext at any given time. This is controlled using the System.ServiceModel.ServiceBehaviorAttribute. ConcurrencyMode is the ConcurrencyMode enumeration.
 
WCF concurrency will help us to configure how WCF service instances can serve multiple requests at the same time.
 
There are three basic types of concurrency supported by WCF 4.0:
  1. Single Concurrency Mode
  2. Multiple Concurrency Mode
  3. Reentrant Concurrency Mode

Single Concurrency Mode

  1. [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single)]  
  2. public class Service1 : IService1  
  3. {  
  4.      public string GetData(int value)  
  5.      {  
  6.           return string.Format("You entered: {0}", value);  
  7.      }  
  8. }  
When the service is set to Single Concurrency Mode, each instance context is allowed to have a maximum of one thread processing messages at the same time. In other words, WCF will provide synchronization with the service instance and not allow concurrent calls with a synchronization lock. In short only one request will proceed at any time and the next request must wait until the first request does not proceed.
 
Every incoming request must try to acquire the sync lock; if no lock is found then it allows access to the service and this request makes a sync lock. When finished operations, WCF will unlock the sync lock and allow other requests to come in.
 

Multiple Concurrency Mode

  1. [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]  
  2. public class Service1 : IService1  
  3. {  
  4.     readonly object ThisLock = new object();  
  5.     public string GetData(int value)  
  6.     {  
  7.         string myRetString = string.Empty;  
  8.         lock (this.ThisLock)  
  9.         {  
  10.              myRetString = string.Format("You entered: {0}", value);  
  11.         }  
  12.         return myRetString;  
  13.     }  
  14. }  
When the service is set to Multiple Concurrency Mode, the service allows multiple accesses at the same time. Simply service instance is not associated with any sync lock. So that concurrent calls are allowed on the service instance. WCF does not create any queue for client messages and replays them as soon as they arrive. Each service has multiple threads processing messages concurrently. The service implementation must be thread-safe to use this concurrency mode.
 
With Concurrency Mode Multiple, threads can call an operation at any time. It is our responsibility to guard our state with locks.
 

Reentrant Concurrency Mode

 
The Reentrant concurrency mode is nothing but a modified version of the single concurrency mode. Similar to single concurrency, reentrant concurrency is associated with a service instance and also sync lock. So that a concurrent call on the same instance is never called. In other words multiple calls on the same instance is not allowed.
  1. [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)]  
  2. public class Service3 : IService1  
  3. {  
  4.      public string GetData(int value)  
  5.      {  
  6.           return string.Format("You entered: {0}", value); ;  
  7.      }  
  8. }  
The service instance is single-threaded and accepts reentrant calls. When the reentrant service calls out, the service state must be in a consistent state, because others could be allowed into the service instance while the service calls out.
 
However, if the reentrant service call to another service or a callback, and that call chain (or causality) somehow wind its way back to the service instance.
 
The only case where a service configured with the Single Concurrency Mode can call back to its clients is when the callback contract operation is configured as one-way because there will not be a reply message to contend for the lock.
 
Reference


Similar Articles