Tuesday, 9 June 2015

Single 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 Single, only a single object of service class is created and this object is responsible to handle all requests for the lifetime of application whether the request come from same client or different client.

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









Detail Code Implementation :

In following code value of variable [i] will increase in each call whether call is made from same client or other client.

If you make call from multiple browser, value of [i] will start from last set value of [i].

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

[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
    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 From First browser :

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

Output From Second browser :

Number After first call : 4
Number After first call : 5
Number After first call : 6



Which design is better PerCall service or PerSession service ?


  1. If you prefer using object orientated programming style, then PerSession should be choice. on the other hand if you prefer SOA style, then PerCall should be your choice.
  2. Decision can also be depends on the application architecture, performance and scalability needs.
  3. PerSession service perform better because the service object does not have to be instantiated on subsequent request whereas PerCall service scale better because the service object destroyed immediately after the method call returns.
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 Post : 

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