Service Concurrency Modes

Concurrent access to the service instance is governed by the ConcurrencyMode property of the ServiceBehavior attribute:

public enum ConcurrencyMode
{
   Single,
   Reentrant,
   Multiple
}

[AttributeUsage(AttributeTargets.Class)]
public sealed class ServiceBehaviorAttribute : ...
{
   public ConcurrencyMode ConcurrencyMode
   {get;set;}
   //More members
}

The value of the ConcurrencyMode enum controls if and when concurrent calls are allowed. The name ConcurrencyMode is actually incorrect; the proper name for this property would have been ConcurrencyContextMode, since it synchronizes access not to the instance, but rather to the context containing the instance (much the same way InstanceContextMode controls the instantiation of the context, not the instance). The significance of this distinction—i.e., that the synchronization is related to the context and not to the instance—will become evident later.

ConcurrencyMode.Single

When the service is configured with ConcurrencyMode.Single, WCF will provide automatic synchronization to the service context and disallow concurrent calls by associating the context containing the service instance with a synchronization lock. Every call coming into the service must first try to acquire the lock. If the lock is unowned, the caller will be allowed in. Once the operation returns, WCF will unlock the lock, thus allowing in another caller.

The important thing is that only one caller at a time is ever allowed. If there are multiple concurrent ...

Get Programming WCF Services, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.