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:

Single Concurrency Mode

Multiple Concurrency Mode

Reentrant Concurrency Mode

Single Concurrency Mode

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single)]

publicclass Service1 : IService1

{

publicstring GetData(int value)

{

returnstring.Format("You entered: {0}", value);

}

}

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

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]

publicclass Service1 : IService1

{

readonlyobject ThisLock = newobject();

publicstring GetData(int value)

{

string myRetString = string.Empty;

lock (this.ThisLock)

{

myRetString = string.Format("You entered: {0}", value);

}

return myRetString;

}

}

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.

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)]

publicclass Service3 : IService1

{

publicstring GetData(int value)

{

returnstring.Format("You entered: {0}", value); ;

}

}

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.