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|
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|
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.
Reference books, you may like:Programming WCF Services: Design and Build Maintainable Service-Oriented Systems
WCF Multi-Layer Services Development with Entity Framework, 4th Edition
Learning WCF: A Hands-on Guide
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.