WCF Concurrency – Easy WCF
Skip to content

WCF Concurrency

Multiple threads executing the application code simultaneously is called Concurrency.

Single Concurrency Mode:

The default concurrency mode is Single in WCF.

This means only a single thread can access the service instance at any given point of time.

While a request is being processed by the service instance, an exclusive lock is acquired and all the other threads will have to wait until the current request completes and the lock is released.Upon releasing the lock, next thread in the queue can access the service instance.

ConcurrencyMode parameter of the ServiceBehavior attribute controls the concurrency setting.Concurrency mode can be Single,Reentrant or Multiple.

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode .Single)]

Whether a WCF service handles client requests concurrently or not depends on 3 things.

  • Service InstanceContextMode
  • Service ConcurrencyMode
  • If binding supports session or not
InstanceContextMode ConcurrencyMode Binding supports session Concurrent calls processed
PerCall Single No Yes
PerCall Single Yes No
PerSession Single No Yes
PerSession Single Yes Yes
Single Single No No
Single Single Yes No

Multiple Concurrency Mode:

With multiple concurrency mode, an exclusive lock is not acquired on the service instance.

This means multiple threads are allowed to access the service instance simultaneously and we get better throughput and shared resources must be protected from concurrent access by multiple threads to avoid concurrency issues.

When concurrency mode is set to Multiple, requests are processed concurrently by the service instance regardless of service instance context mode and binding supports on session.

InstanceContextMode ConcurrencyMode Binding supports session Concurrent calls processed Throughput Impact
PerCall Multiple No Yes Positive
PerCall Multiple Yes Yes Positive
PerSession Multiple No Yes Positive
PerSession Multiple Yes Yes Positive
Single Multiple No Yes Positive
Single Multiple Yes Yes Positive

Reentrant Concurrency Mode:

Reentrant concurrency mode allows WCF service to issue callbacks to the client application.

While the service instance is processing the client request, the service wants to callback the client.

The callback operation is not oneway.This means the response for the call back from client needs to get back same service instance but the service instance is locked and the response from the client cannot reenter and access the service instance.This leads to deadlock.

There are 2 ways to resolve Deadlock.

  • Set the ConcurrencyMode of WCF service to Reentrant
    • [ServiceBehavior (ConcurrencyMode=  ConcurrencyMode.Reentrant)]
  • Make the call back operation Oneway
    • [OperationContract(IsOneWay = true)]

© 2015, admin. All rights reserved.

One thought on “WCF Concurrency

  1. Pingback: WCF Interview Questions (Part – 2) | EasyWCF

Leave a Reply

Your email address will not be published. Required fields are marked *