Sunday, August 9, 2015

Sessions with WCF Service

Sessions in WCF refers controlling correlation of messages between two end points.

Session Modes
  • Allowed: Clients can connect with or without establishing session 
  • Required: Client can connect only with establishing session 
  • NotAllowed: Client can connect only without session
    [ServiceContract(SessionMode= SessionMode.Required)]
    public interface IService1
    {
        [OperationContract]
        string GetData();
    }

Session is not supported by all bindings. Only WS-*, netTcpBinding & netNamedPipe are support wcf sessions.

If we use bindings other than mentioned above, service throws exception






Instance Context Modes
  • PerCall: Each request made to the service is served by a new instance of service
  • PerSession: All requests made to the service served by a single service instance for a particular single session
  • Single: All request made to the service served by a single service instance even from different clients
   [ServiceBehavior(InstanceContextMode= InstanceContextMode.PerSession)]
    public class Service1 : IService1
    {
       int counter = 0;
        public string GetData()
        {
            return string.Format("Counter : {0} , Session Id: {1}", counter++, OperationContext.Current.SessionId);
        }
    }

Below are the results of 9 scenarios

Session Mode : Allowed

1. InstanceContextMode.PerCall : Session maintained but new instance serving every method call










2. InstanceContextMode.PerSession : Session maintained with every client and only one instance serving all calls for a particular session with a client










3. InstanceContextMode.Single : Session maintained with every client only one service instance serving all service calls from all clients doesn't matter session



























Session Mode : NotAllowed
1. InstanceContextMode.PerCall : Session not maintained every call served by new instance


















2. InstanceContextMode.PerSession : Session not maintained every call served by new instance



















3. InstanceContextMode.Single : Session not maintained every call to the service is served by single instance of service to all clients
























Session Mode :Required

1. InstanceContextMode.PerCall : Session maintained every call served by new instance

















2. InstanceContextMode.PerSession : Session maintained with every client & calls to the service served by one instance within established particular sessions between clients and service


















3. InstanceContextMode.Single : Session maintained between clients and service & only one instance serving all calls to the service from all the clients.





















Important Links:
http://devproconnections.com/net-framework/load-balancing-and-scaling-your-wcf-services

No comments:

Post a 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...