WCF Concurrency Mode


Introduction

WCF Concurrency modes gives us the possibility of taking control over these thread creation process so that a WCF service creator can take better, desirable and perhaps more control on the WCF service behavior. This article explains about different WCF concurrency mode and their impact with service when we are using concurrency mode.

Getting Started

WCF concurrency mode controls active threads in an InstanceContext at any given time. This concurrency is controlled under System.ServiceModel.ServiceBehaviorAttribute class. Thread is working in service such a way like Whenever a client calls a WCF service function this instance of the WCF service class is created on a thread that is taken from a thread pool. Once the instance is used and no longer needed, the thread will be returned to the thread pool so that it can be reclaimed for further requests. These thread creation is controlled by concurrency mode of WCF, it also takes care how many thread should creates per instance, for a instance or call etc .

There are three types of Concurrency mode in WCF service that are listed below

  1. Single
  2. Multiple
  3. Reentrant

Single Concurrency Mode

This is the default mode and this mode is safest from the concurrency perspective as the developer will not have to take care of concurrency issues in the code.When the service is set to Single Concurrency Mode, each instance context(Single,PerCall,PerSession) is allowed to have a maximum of one thread processing messages at the same time.

If any thread has already created an instance of the class then further requesting threads can create the instance but they will have to wait for first thread to finish using its instance and then it will get a change to execute the operation on its instance.

 [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single)]  
 public class Service1 : IService1  
 {  
 }  

Multiple Concurrency Mode

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.

In Multiple mode, each thread will have its own instance of the WCF service object and they all will be executing at the same time. Now this is the most complicated mode because all the concurrency issues must be handled by the service.

Thread behaviors in different InstanceContextMode

  • Single:- Multiple threads will be created for all the clients.
  • PerSession:- Mulitple threads can be created for each client request.
  • PerCall:- A single thread will be created for each call process.

 [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]  
 public class Service1 : IService1  
 {  
 }  

Reentrant Concurrency Mode

Reenatrant Concurrency Mode is quire similar to Single Concurrency Mode , you can say it is the modified version of Single Concurrenct Mode and allows only one thread to process the instance at a time. Since this is similar to Single mode and allows only one thread to process the instance at a time for each instance modes(Single,PerSession,PerCall), but as soon as the instance calls any other service or go for processing client callbacks, the other thread will get a chance to execute its own instance of WCF service class.

 [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)]  
 public class Service1 : IService1  
 {  
 }  

Summary

In the above of this article we have discussed about different concurrent mode WCF service, Hope you have got details.

Thanks
Kailash Chandra Behera


No comments:

Post a Comment