Instance Management

Instance management in WCF is the concept in which we can understand the instance creation of service when client request for the service. We have to set the InstanceContextMode in ServiceBehavior. There are three type of instance management available in WCF and these are as follows:-

Per Call Instance Management

In per call instance management every time we hit the service means when we call method it will simply create the new instance of service and execute the method and give the result to the client. After that it will simply dispose the instance. There are no overhead on the server in this type instance management as resources deallocate as the user request complete. At every method call new instance will be created in per call.

For Example
		
[ServiceContract]
public interface IService
{

	[OperationContract]
	int GetData();

	
}
		
		
		
Service Class code 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall)]
public class Service : IService
{
    int x = 0;

	public int GetData()
	{
        x++;
        return x;

	}

	
}
		
		

Web config code
Add this code inside the System.ServiceModel tag

		
		<services>
      <service name="Service">
        <endpoint address="" binding="basicHttpBinding" contract="IService"/>
      </service>
</services>	

		
		

As you can see the binding that I implemented the basichttpbinding and now create the client for this service:-

		
    ServiceReference1.ServiceClient svc = new ServiceReference1.ServiceClient();
        
    Response.Write("Method Calling First Time and Result is=" + svc.GetData() + "<br>");
    Response.Write("Method Calling Second Time and Result is=" + svc.GetData() + "<br>");
    Response.Write("Method Calling Third Time and Result is=" + svc.GetData() + "<br>");
    Response.Write("Method Calling Fourth Time and Result is=" + svc.GetData() + "<br>");
		
		

Now you can see the output of this code in the following image:-

per call instance management

As you can see for every calling it will give the same result as every time when I call the method it will create the new instance of the service and accordingly giving the result

Per Session Instance Management

Now move to the per session mode. In per session mode service holds the object for single instance of proxy which created at client level. In the following example I have created the two proxy object at the client level. For every instance of proxy service will holds the instance at service level and will give result accordingly.

This is the by default instance mode set in the wcf service. If we do not set any instance mode behaviour then by default it will take per Session.

Example

Service implementation of per session:-

		
		[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]
public class Service : IService
{
    int x = 0;

	public int GetData()
	{
        x++;
        return x;

	}

}

		
		

Web configuration setting for Per Session

		
		<services>
      <service name="Service">
        <endpoint address="" binding="wsHttpBinding" contract="IService"/>
      </service>
</services>
	
		

To implement the per session instance management we have to change the binding as basichttpbinding do not support the per session instance mode.

Now update the service reference at client side and again execute the code and you will get following output:-

At client side create two instances of proxy and call methods

		
		//proxy instance creation
  
ServiceReference1.ServiceClient svc = new ServiceReference1.ServiceClient();

        
        Response.Write("Method Calling First Time with First instance and Result is=" + svc.GetData() + "<br/>");
        Response.Write("Method Calling Second Time with First instance and Result is=" + svc.GetData() + "<br/>");
        Response.Write("Method Calling Third Time with First instance and Result is=" + svc.GetData() + "<br/>");
        Response.Write("Method Calling Fourth Time with First instance and Result is=" + svc.GetData() + "<br/>");

        ServiceReference1.ServiceClient svc1 = new ServiceReference1.ServiceClient();

        Response.Write("<br/><br/>Method Calling First Time with Second instance and Result is=" + svc1.GetData() + "<br/>");
        Response.Write("Method Calling Second Time with Second instance and Result is=" + svc1.GetData() + "<br/>");
        Response.Write("Method Calling Third Time with Second instance and Result is=" + svc1.GetData() + "<br/>");
        Response.Write("Method Calling Fourth Time with Second instance and Result is=" + svc1.GetData() + "<br/>");
	
		

Output

per session instance management

As we can see that service maintain session for each instance and work accordingly.

Single Instance Management

In single type of instance management service will create single object of service for all clients.

Example

		
Set instance mode single 
[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
public class Service : IService
{
    int x = 0;

	public int GetData()
	{
        x++;
        return x;

	}

}

		
		

Update the service reference and execute the client application and see the difference:-

Single instance management

As we can see that for single client service created a single object and we are getting this output.

Now I have created one more .net application means one more client and added the service reference and call the method and I got the following output:-

single instance management output