Tuesday, 9 June 2015

PerSession instance context mode in WCF

This post is continuation of post Instance Context Mode in WCF. Please visit following post before reading this post.
http://logicsmaze.blogspot.in/2015/06/instance-context-mode-in-wcf.html

If the instance context mode in WCF is set to PerSesion, a new instance of the service object is created for each new client session and maintained for the duration of the session.

To specify the PerSession instancing mode you have to set InstanceContextMode attribute of [ServiceBehavior] with PerSession enumeration.









Default session time out is 10 minute.

If you want to Increase/decrease session time out, make following changes in config file.






Note : Once the session is time out, connection of service is closed.As a result, communication channel gets faulted and the client can no longer use the same proxy to communicate with service.

Note : If binding does not support session then the service behave as a PerCall service.
If you are using bascHttpBinding, basicHttpBinding does not support session so client call will be same as PerCall service and state of service object will not be persist.

Implications of PerSession WCF Service :

  1. State maintain between call.
  2. Greater memory consumption as service objects remains in memory until the client session times out.
  3. Concurrency is an issue for multi-threaded clients

Detail Code Implementation :

In following code value of variable [i] will increase in each call.

If you make call from other browser, new session will be created for that client and value of variable [i] will start from 1 and increase in each call.

[ServiceContract]
    public interface IService1
    {
        [OperationContract]
        int IncrementNumber();
     
    }

[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]
    public class Service1 : IService1
    {
        private int _Number;
        public int IncrementNumber()
        {
            _Number = _Number + 1;
             return _Number;
        }
}

Client Code :

protected void Page_Load(object sender, EventArgs e)
    {
         int i;
         ServiceReference1.Service1Client ocjClient = new ServiceReference1.Service1Client();

         i = ocjClient.IncrementNumber();
         Response.Write("Number After first call :" + i.ToString());

         i = ocjClient.IncrementNumber();
         Response.Write("Number After Second call :" + i.ToString());

         i = ocjClient.IncrementNumber();
         Response.Write("Number After Thisrd call :" + i.ToString());
    }

Output :

Number After first call : 1
Number After first call : 2
Number After first call : 3

Visit following post for detailed implementation of PerCall , PerSession  and Single instance context mode.

Instance-context-mode-in-wcf
PerCall-instance-context-mode-in-wcf
PerSession-instance-context-mode-in-wcf
Single-instance-context-mode-in-wcf.html

Related Topics :

Message-exchange-pattern-in-wcf
Different-option-of-hosting-wcf-service
Binding-in-wcf-choosing-right-wcf-service
Hosting WCF with Non-Http protocol
Exchanging-metadata-in-wcf
Some-interesting-facts-about-data-contract
Knowntype-attribute-in-wcf
Associating-knowntype-in-wcf
Message-contract-in-wcf
Exception-handling-in-wcf
Exception Handling in WCF - SOAP Fault in WCF
Exception Handling in WCF - Unhandled Exception in WCF
Exception Handling in WCF - Creating and Throwing Strongly Typed SOAP Fault

No comments:

Post a Comment