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:
public enum InstanceContextMode
{
PerCall,
PerSession,
Single
}
Instance Modes available in WCF are:
- PerCall
- PerSession
- Single
PerCall Services
When a service is configured PerCall, every client gets a new service instance. The above diagram shows how a PerCall instance works:
- Client calls the proxy and proxy forwards the call to service
- WCF creates an instance and calls the method call.
- 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:
[ServiceContract]
public interface IService1
{
[OperationContract]
int RetVal();
}
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.
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class Service1 : IService1
{
private int _counter;
public int RetVal()
{
_counter++;
return _counter;
}
}
Step 3: When we invoke this method with the client, each time we get the same value.
var proxy = new Service1Client();
MessageBox.Show(proxy.RetVal().ToString());
MessageBox.Show(proxy.RetVal().ToString());
Benefits of PerCall
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.
[ServiceContract]
interface IMyContract
{...}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
class MyService : IMyContract
{...}
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:
Let us understand Persession service instance mode with example.
Step 1: We just change InstanceContextMode PerCall to PerSession in the above example.
[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.
var proxy = new Service1Client();
MessageBox.Show(proxy.RetVal().ToString());
MessageBox.Show(proxy.RetVal().ToString());
Benefit of PerSession
The state is maintained by a service instance.
Drawbacks of PerSession
-
Less throughput, greater memory consumption
-
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:
Let us understand singleton Instance Service mode with an example.
Step 1: We just change InstanceContextMode to Single.
[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:
var proxy = new Service1Client();
MessageBox.Show(proxy.RetVal().ToString());
MessageBox.Show(proxy.RetVal().ToString());
Output
Benefit of Single Instance:
State is maintained by the service instance.
Drawbacks of Single Instance
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!!!