Tuesday, 9 June 2015

PerCall 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


Whenever the instance context mode for WCF service is set to PerCall, a new instance of the WCF service object is created for every request whether the request come from same client or a different client.

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










Implications of PerCall WCF Service :


  1. Better memory usage as service object freed immediately after the method call returns.
  2. Concurrency not an issue
  3. Application scalability is batter
  4. State not maintained between calls
  5. Performance could be an issue as there as overhead involved in reconstructing the service instance state on each and every memory call.


Detail Code Implementation :

In following code value of variable [i] will be 1 for each call.

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

 [ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall)]
    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 : 1
Number After first call : 1

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


Instance-context-mode-in-wcf





No comments:

Post a Comment