Saturday, 17 January 2015

WCF Instance Management





Instance management refers to the way a service handles a request from a client. Instance management is set of techniques WCF uses to bind client request to service instance, governing which service instance handles which client request. It is necessary because application will differ in their need for scalability, performance, durability, transaction and queued calls.
Basically there are three instance modes in WCF:
·         Per-Call instance mode
·         Per-Session instance mode
·         Singleton Instance Mode




Configuration
Instance mode can be configured using ServiceBehavior attribute. This can be specified at implementing the service contract as shown below.
 [ServiceContract()]
    public interface IMyService
    {
        [OperationContract]
        int MyMethod();
    }
  
    [ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
    public class MyService:IMyService
    {
        public int MyMethod()
        {
            //Do something
        }      
    }

Per-Call Service

When WCF service is configured for Per-Call instance mode, Service instance will be created for each client request. This Service instance will be disposed after response is sent back to client.


Following diagram represent the process of handling the request from client using Per-Call instance mode.


Let as understand the per-call instance mode using example.
Step 1: Create the service contract called IMyService and implement the interface. Add service behavior attribute to the service class and set the InstanceContextMode property to PerCall as show below.
[ServiceContract()]
public interface IMyService
{
        [OperationContract]
        int MyMethod();
}
 
Step 2: In this implementation of MyMethod operation, increment the static variable (m_Counter). Each time while making call to the service, m_Counter variable is incremented and return the value to the client.
[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall)]
public class MyService:IMyService
{
        static int m_Counter = 0;
 
        public int MyMethod()
        {
            m_Counter++;
            return m_Counter;
        }       
}
 
Step 3: Client side, create the proxy for the service and call "MyMethod" operation multiple time.
static void Main(string[] args)
{
        Console.WriteLine("Service Instance mode: Per-Call");
        Console.WriteLine("Client making call to service...");
 
        //Creating the proxy on client side
        MyCalculatorServiceProxy.MyServiceProxy proxy = new MyCalculatorServiceProxy.MyServiceProxy();
 
        Console.WriteLine("Counter: " + proxy.MyMethod());
        Console.WriteLine("Counter: " + proxy.MyMethod());
        Console.WriteLine("Counter: " + proxy.MyMethod());
        Console.WriteLine("Counter: " + proxy.MyMethod());
        
        Console.ReadLine();
}
 
Surprisingly, all requests to service return '1', because we configured the Instance mode to Per-Call. Service instance will created for each request and value of static variable will be set to one. While return back, service instance will be disposed. Output is shown below.



Per-Session Service

When WCF service is configured for Per-Session instance mode, logical session between client and service will be maintained. When the client creates new proxy to particular service instance, a dedicated service instance will be provided to the client. It is independent of all other instance.
Following diagram represent the process of handling the request from client using Per-Session instance mode.


Let as understand the Per-Session instance mode using example.
Step 1: Create the service contract called IMyService and implement the interface. Add service behavior attribute to the service class and set the InstanceContextMode property to PerSession as show below.
[ServiceContract()]
public interface IMyService
{
        [OperationContract]
        int MyMethod();
}
 
Step 2: In this implementation of MyMethod operation, increment the static variable (m_Counter). Each time while making call to the service, m_Counter variable will be incremented and return the value to the client.
[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]
public class MyService:IMyService
{
        static int m_Counter = 0;
 
        public int MyMethod()
        {
            m_Counter++;
            return m_Counter;
        }       
}
 
Step 3: Client side, create the proxy for the service and call "myMethod" operation multiple time.
static void Main(string[] args)
{
        Console.WriteLine("Service Instance mode: Per-Session");
        Console.WriteLine("Client making call to service...");
 
        //Creating the proxy on client side
        MyCalculatorServiceProxy.MyServiceProxy proxy = new MyCalculatorServiceProxy.MyServiceProxy();
 
        Console.WriteLine("Counter: " + proxy.MyMethod());
        Console.WriteLine("Counter: " + proxy.MyMethod());
        Console.WriteLine("Counter: " + proxy.MyMethod());
        Console.WriteLine("Counter: " + proxy.MyMethod());
 
        Console.ReadLine();
}
 
All request to service return incremented value (1, 2, 3, 4), because we configured the instance mode to Per-Session. Service instance will be created once the proxy is created at client side. So each time request is made to the service, static variable is incremented. So each call to MyMethod returns incremented value. Output is shown below.

 

Singleton Service

When WCF service is configured for Singleton instance mode, all clients are independently connected to the same single instance. This singleton instance will be created when service is hosted and, it is disposed when host shuts down.
Following diagram represent the process of handling the request from client using Singleton instance mode.


Let as understand the Singleton Instance mode using example.
Step 1: Create the service contract called IMyService and implement the interface. Add service behavior attribute to the service class and set the InstanceContextMode property to Single as show below.
[ServiceContract()]
public interface IMyService
{
        [OperationContract]
        int MyMethod();
}
 
Step 2: In this implementation of MyMethod operation, increment the static variable (m_Counter). Each time while making call to the service, m_Counter variable is incremented and return the value to the client.
[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
public class MyService:IMyService
{
        static int m_Counter = 0;
 
        public int MyMethod()
        {
            m_Counter++;
            return m_Counter;
        }       
}
 
Step 3: Client side, create the two proxies for the service and made a multiple call to MyMethod.
static void Main(string[] args)
{
        Console.WriteLine("Service Instance mode: Singleton");
        Console.WriteLine("Client 1 making call to service...");
        
        //Creating the proxy on client side
        MyCalculatorServiceProxy.MyServiceProxy proxy = new MyCalculatorServiceProxy.MyServiceProxy();
 
        Console.WriteLine("Counter: " + proxy.MyMethod());
        Console.WriteLine("Counter: " + proxy.MyMethod());
        Console.WriteLine("Counter: " + proxy.MyMethod());
 
        Console.WriteLine("Client 2 making call to service...");
        
        //Creating new proxy to act as new client
        MyCalculatorServiceProxy.MyServiceProxy proxy2 = new MyCalculatorServiceProxy.MyServiceProxy();
 
        Console.WriteLine("Counter: " + proxy2.MyMethod());
        Console.WriteLine("Counter: " + proxy2.MyMethod());
        Console.ReadLine();
}
               
When two proxy class made a request to service, single instance at service will handle it and it return incremented value (1, 2, 3, 4), because instance mode is configured to 'Single'. Service instance is created when it is hosted. So this instance will remain till host is shutdown. Output is shown below.





No comments:

Post a Comment