Concurrency Mode In WCF

Introduction

WCF Concurrency modes give 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 different WCF concurrency mode and their impact on 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. This thread creation is controlled by concurrency mode of WCF, it also takes care of how many threads should create per instance, or per 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 the first thread to finish using its instance and then it will get a chance 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 model.

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

An IT Professional with 12 years experience in development life cycle in windows, service and Web based application using Microsoft.Net technologies. Proven record of developing all phases of projects in Microsoft.Net technology from initiation to closure aligning with the company's Business objectives to drive process improvements, competitive advantage and bottom-line gains. -> Good exposure of independently working and developing multiple projects ->Committed to efficient and effective development of projects in a fast-paced and deadline driver environment. Skill :- Develop and design projects in various technologies of Microsoft Technology. Total IT Experience- 13+

Previous Post Next Post

نموذج الاتصال