WCF Instance Management

What is Instance Management?

 
Instance Management is the name for a set of techniques used by WCF to bind client requests to service instances, governing which service instance handles which client request. This is a service side implementation detail. Reference: Programming WCF Services
 

Why we need Instance Management

 
We need Instance Management because applications differ too much in their needs for scalability, performance, throughput, transactions and queued calls. So one solution for all applications doesn't fit.
 
3 ways of instance management
 
WCF framework has provided 3 ways by which we can manage WCF service instance creation.
  • Per-Call
  • Per-Session
  • Single

Per-Call instance mode

 
One instance of the service class is created per incoming request. A service instance is created only when the client call is in progress and it will be destroyed after response is sent back to client.
 
Every time the client sends a new request either from the same proxy or a different proxy, a new service instance is always created.
 
Per-Call instance mode in WCF 
 
This represents the process of handling the request from client using Per-Call instance mode.
 
WCF Per-Call instance mode 
 

Implementation of Per-Call services


To configure a service type as a per-call service, we need to apply the 'ServiceBehavior' attribute with the 'InstanceContextMode' property set to InstanceContextMode.PerCall.
 
- Service code -
  1. [ServiceContract]   
  2. interface IMyContract   
  3. {   
  4.     [OperationContract]   
  5.     int MyMethod( );   
  6. }   
  7. [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]   
  8. class MyService : IMyContract,IDisposable   
  9. {  
  10.     static int m_Counter = 0;  
  11.     public int MyMethod( )  
  12.     {   
  13.         m_Counter++;   
  14.         return m_Counter;   
  15.     }  
  16. }
- Client code -
  1. MyContractClient proxy = new MyContractClient( );  
  2. Console.WriteLine("Counter: " + proxy.MyMethod());  
  3. Console.WriteLine("Counter: " + proxy.MyMethod());  
  4. Console.WriteLine("Counter: " + proxy.MyMethod());  
  5. Console.WriteLine("Counter: " + proxy.MyMethod());  
  6. Console.ReadLine();  
Possible Output
 
Counter: 1
Counter: 1
Counter: 1
Counter: 1
 
Important points of per-call instance mode
  • Service instance exists only when client call is in progress. It is allocated when a client makes a request and deallocated when response is sent back to the 
    client.

Per-Session instance mode

 
One instance of the service class is created per client session. Here, a logical session is maintained between client and service.
 
Each proxy will connect to new instance.
 
WCF Per-Session instance mode 
 

Implementation of Per-Session services

 
To configure a service type as a per-session service, we need to apply the 'ServiceBehavior' attribute with the 'InstanceContextMode' property set to InstanceContextMode.PerSession.

- Service code -
  1. [ServiceContract]   
  2. interface IMyContract   
  3. {   
  4.     [OperationContract]   
  5.     int MyMethod( );   
  6. }   
  7. [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]   
  8. class MyService : IMyContract,IDisposable   
  9. {  
  10.     static int m_Counter = 0;  
  11.     public int MyMethod( )  
  12.     {   
  13.         m_Counter++;   
  14.         return m_Counter;   
  15.     }  
  16. }
- Client code -
  1. MyContractClient proxy = new MyContractClient( );  
  2. Console.WriteLine("Counter: " + proxy.MyMethod());  
  3. Console.WriteLine("Counter: " + proxy.MyMethod());  
  4. Console.WriteLine("Counter: " + proxy.MyMethod());  
  5. Console.WriteLine("Counter: " + proxy.MyMethod());  
  6. Console.ReadLine();  
Possible Output
 
Counter: 1
Counter: 2
Counter: 3
Counter: 4
 
Important points of per-session instance mode
  • Each proxy will be connected to a new dedicated service instance.
  • Service instance is allocated when client creates a new proxy and deallocated when client destroys this proxy object.
  • Client session is per service endpoint per proxy.

Single instance mode

 
One instance of the service class handles all incoming requests. This implements a singleton. When a service is configured as a singleton, all clients connect to the same shared service instance; it is not a factor, to which endpoint of the service they are connected.
 
All client requests will go to same instance.
 
WCF Single instance mode 
 

Implementation of Single instance services

 
To configure a service type as a single instance service, we need to apply the 'ServiceBehavior' attribute with the 'InstanceContextMode' property set to
InstanceContextMode.Single.
 
- Service code -
  1. [ServiceContract]   
  2. interface IMyContract   
  3. {   
  4.     [OperationContract]   
  5.     int MyMethod( );   
  6. }   
  7. [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]   
  8. class MyService : IMyContract,IDisposable   
  9. {  
  10.     static int m_Counter = 0;  
  11.     public int MyMethod( )  
  12.     {   
  13.         m_Counter++;   
  14.         return m_Counter;   
  15.     }  
  16. }   
- Client code -
  1. Console.WriteLine("Client 1 making call to service.");  
  2. Console.WriteLine("Counter: " + proxy1.MyMethod());  
  3. Console.WriteLine("Counter: " + proxy1.MyMethod());  
  4. Console.WriteLine("Counter: " + proxy1.MyMethod());  
  5.   
  6. Console.WriteLine("Client 2 making call to service.");  
  7. MyContractClient proxy2 = new MyContractClient( );  
  8.   
  9. Console.WriteLine("Counter: " + proxy2.MyMethod());  
  10. Console.WriteLine("Counter: " + proxy2.MyMethod());  
  11. Console.ReadLine();
Possible Output
 
Client 1 making call to service....
 
Counter: 1
Counter: 2
Counter: 3
 
Client 2 making call to service....
 
Counter: 4
Counter: 5
 
Important points of Single instance mode
  • Only one service instance is created. It is created when the service host is created.
  • This service instance lives forever and is only destroyed when service host shuts down.
  • Only one service instance is created. It doesn't matter whether the requests coming from different endpoints or different proxies.
When to use per-call, per-session and single instance mode
 
Different instance mode in WCF 


Similar Articles