Monday, August 10, 2015

Concurrency in WCF Services

Concurrency in WCF refers controlling number of threads in a given session context. ConcurrencyMode can be used with ServiceBehavior attribute.

Concurrency Modes
  • Single: Only one thread allowed to run in a session context, multiple calls to service served one by one.
  • Multiple: Multiple threads allowed to run in a session context so multiple calls to the service could be served simultaneously, calls could be from same client or different clients.To use this mode service should be designed thread safe. Operations on object's states(operations changing state of objects) should be locked, so that other thread couldn't access those in between operations.
  • Reentrant:  This is modified mode of 'Single'. Only one thread is allowed to run, any other call to the service in queue may start to serve when current one calls some other wcf service.So when proceed thread calls some other wcf service the thread released locks and other queued call may start.

     [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string GetData();
    }
    [ServiceBehavior(InstanceContextMode= InstanceContextMode.PerCall, ConcurrencyMode= ConcurrencyMode.Multiple)]
    public class Service1 : IService1
    {
        int instanceCount =0;
        object thislock = new object();
        public string GetData()
        {
            lock (thislock) //Loc is important when you using "Multiple" concurrency mode
            {
            Thread.Sleep(1000);
            return string.Format("Instance Count:{0}, Session Id:{1}, Thread Name: {2}, Time: {3}", ++instanceCount, OperationContext.Current.SessionId, Thread.CurrentThread.Name, DateTime.Now.ToString());
            }
        }    
    }



Client Code

  class Program
  {
    static void Main(string[] args)
     {
       ServiceReference1.Service1Client _client = new ServiceReference1.Service1Client();
       Console.WriteLine("Service started listening clients....");
       for(int i=0; i<4; i++)
       _client.GetData();
       Console.ReadLine();
     }
  }

InstanceContextMode  Vs
ConcurrencyMode
PerCall
Single Thread for all calls
Single Thread for all calls
Single Thread for every call lock released for other calls if other WCF service call made
PerSession
Single thread for every particular session between service and client
Multiple Threads for every particular session between service and client
Single Thread for every call lock released for other calls if other WCF service call made
Single
Single Thread for calls from clients
Multiple Threads for calls from clients
Single Thread for every call lock released for other calls if other WCF service call made


  • Same thread id 37 serving calls from multiple clients, all calls executed in queue 5 sec time difference seen(Thread.Sleep(5000) was given for these screenshot)




  • Multiple threads for different calls from different clients




















  • At same time different calls execution (because of external WCF call thread released & other queued call served)











1 comment:

CI/CD - Safe DB Changes/Migrations

Safe DB Migrations means updating your database schema without breaking the running application and without downtime . In real systems (A...