Reader Level:
ARTICLE

WCF Concurrency

Posted by Jignesh Trivedi Articles | WCF with C# June 15, 2012
In this article I will explain WCF Concurrency with ConcurrencyMode with the ConcurrencyMode enumeration.
  • 0
  • 0
  • 17718

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

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single)]
public class Service1 :
IService1
{
     public string GetData(int value)
     {
          return string.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)]
public class Service1 :
IService1
{
    readonly object ThisLock = new object();
    public string 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)]
public class Service3 :
IService1
{
     public string GetData(int value)
     {
          return string.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.

Reference

http://blogs.msdn.com/b/rickrain/archive/2009/06/17/wcf-instancing-concurrency-and-throttling-part-2.aspx?Redirected=true
http://msdn.microsoft.com/en-us/library/ms731193.aspx

COMMENT USING

Trending up