Instance Modes In WCF

Instance mode is basically a service side implementation detail. It should not manifest itself on the client side. WCF defines the behaviors. Behavior is a local attribute of a service or client that does not affect communication. The client never knows about the service behavior and does not manifest service binding or published metadata.

For service instance mode we use the ServiceBehaviour attribute that defines the InstanceContextMode property of the enum type InstanceContextMode. the value of the InstanceContextMode enum defines which instance mode is used for the service.

InstanceContextMode
  1. public enum InstanceContextMode  
  2. {  
  3.     PerCall,  
  4.     PerSession,  
  5.     Single  
  6. }  
Instance Modes available in WCF are
  1. PerCall
  2. PerSession
  3. Single

PerCall Services

Instance Modes in WCF
When a service is configured PerCall, every client gets a new service instance. The above diagram shows how a PerCall instance works
  1. Client calls the proxy and proxy forwards the call to service
  2. WCF creates an instance and calls the method call.
  3. When method call returns, WCF calls IDisponsible if implemented.
The following explains the PerCall Instance method with an example.

Step 1
 
Create a service with a default contract name, in other words IService1, and implement the interface as in the following
  1. [ServiceContract]  
  2. public interface IService1  
  3. {  
  4.     [OperationContract]  
  5.     int RetVal();  
  6. }  
Step 2
 
In the implementation of the RetVal operation, increment the "_counter". When the client calls the service, the "_counter" variable is incremented and returned to the client.
  1. [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]  
  2.   
  3. public class Service1 : IService1  
  4.   
  5. {  
  6.   
  7.     private int _counter;  
  8.   
  9.     public int RetVal()  
  10.   
  11.     {  
  12.   
  13.         _counter++;  
  14.   
  15.         return _counter;  
  16.   
  17.     }  
  18.   
  19. }  
Step 3
 
When we invoke this method with the client, each time we get the same value.
  1. var proxy = new Service1Client();  
  2. MessageBox.Show(proxy.RetVal().ToString());  
  3. MessageBox.Show(proxy.RetVal().ToString());  
Benefits of PerCall
  • Less Memory consumption
  • Increased throughput
  • Concurrency is not an issue
Drawback of PerCall

State is not mentioned between calls.

PerCall Configuration

To Configure a Service type as PerCall, we can apply the ServiceBehaviour attribute with InstanceContextMode Property as PerCall.
  1. [ServiceContract]  
  2. interface IMyContract  
  3. {...}  
  4. [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]  
  5. class MyService : IMyContract  
  6. {...}   

PerSession Service


WCF maintains a logical session between client and service in Persession instance mode. A new instance of the service class is created for each client.

The following given diagram represents the request from the client using Persession service instance mode 
Instance Modes in WCF

Let us understand Persession service instance mode with example.

Step 1
 
We just change InstanceContextMode PerCall to PerSession in the above example.
  1. [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]  
Step 2
 
When we run the following code on the client side, we see the value 2 after the final client code is executed. If we call the method two times then the Value will be shown as 2.
  1. var proxy = new Service1Client();  
  2. MessageBox.Show(proxy.RetVal().ToString());  
  3. MessageBox.Show(proxy.RetVal().ToString());  
Benefit of PerSession

The state is maintained by a service instance.

Drawbacks of PerSession
  1. Less throughput, greater memory consumption
  2. Concurrency issues for multithreaded clients

Single Instance Service


When we configure a service as a singleton, all clients are connected to a single instance context. We configure a singleton service by setting the InstanceContextMode property as single. Only one instance is created upon creation of a service host. This instance is forever for the service. Another service instance is created only when we reset the IIS or when the service host is shut down.

Diagrammatic representation of a singletion instance

Instance Modes in WCF
Let us understand singleton Instance Service mode with an example.

Step 1
 
We just change InstanceContextMode to Single.
  1. [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]  
Step 2
 
When two clients request a single instance service, it returns an incremental value, in other words 1 or 2. This is because service instance mode is single. The first client is a WCF Client and the second client is a Windows Application Client. The code for the Windows client is as follows
  1. var proxy = new Service1Client();  
  2. MessageBox.Show(proxy.RetVal().ToString());  
  3. MessageBox.Show(proxy.RetVal().ToString());  
Output

Instance Modes in WCF

Benefit of Single Instance

State is maintained by the service instance.

Drawbacks of Single Instance
  • Least throughput
  • Greater memory consumption
  • Concurrency issues
Guidelines for choosing WCF instancing mode
 
Selecting an instance mode always depends on what a service is trying to do. Normally, for scalability and throughput, we prefer to use PerCall services whereever possible. We should use PerSession only when it is necessary, because it overheads the session and session times out. Also we should try to avoid singleton owing to greater memory consumption. Singleton is, however, useful on client machine for shared functionality. These are only general guidelines.

Any recommendation and feedback will be highly appreciated. I hope this article proves useful for you. Thanks for reading!!! 


Similar Articles