In this article, I will demonstrate you about how to create instance of WCF service with different way. Actually in WCF service, we can create instance of the service in three ways and It is Per Call, Per Session and Single. If you want to know only overview, see the following image which will clear your all doubts. 

WCF Instance

Now, it is time to learn step by step about everyone in detail.

Per Call Service

When you configure your service instance mode as Per Call Service then instance creates for every request. It means to say that on every request to access your service will be after new instance creation. When client get back to data from client then the created instance is going to dispose.

WCF Per Call

 

 

 

 

 

 

 

 

 

 

 

 

 

 

So actually what happen from request to get back the data on client? When you call a service through your client then client first goes to proxy class which is able to handle service data or it is for communication with service. 

So, this is responsibility of proxy class to send the request to service and create instance of the WCF Service. So, after creating the instance of the class, it calls the requested method, method returns the value and give it to client and after that the instance of the WCF Service is going to expose. I hope the following image will clear your all doubts about how the Per Call works in real scenario.

Your Service Implementation

[ServiceContract()]  
public interface IMathService  
{  
    [OperationContract]  
    int ReturnCount();  
}  
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]  
public class MathService: IMathService  
{  
    static int counter = 0;  
    public int ReturnCount()  
    {  
        counter++;  
        return counter;  
    }  
}  

Client

public static void Main(string[] args)  
{  
    Console.WriteLine("Service Instance mode: Per Call ");  
    Console.WriteLine("Client making call to service...");  
    MyMathServiceServiceProxy.MyServiceProxy proxy = new MyMathServiceProxy.MyServiceProxy();  
    Console.WriteLine("Count: " + proxy.ReturnCount());  
    Console.WriteLine("Count: " + proxy.ReturnCount());  
    Console.WriteLine("Count: " + proxy.ReturnCount());  
    Console.WriteLine("Count: " + proxy.ReturnCount());  
    Console.ReadLine();  
}  

 

Per Session Service

When you configure your service in Per Session mode, you need to configure the service as per session. In this a client can interact with service using the session instance. Here every request will not create new instance. The service will work on the single session instance. 

So, how this actually process with WCF service? Basically when client request data from service, first it calls the proxy of WCF service and call the method. When WCF service is calling the method, it creates a instance of the WCF service and store it on session. When client again request then the same instance is used to process the same data.

Your Service Implementation

[ServiceContract()]  
public interface IMathService  
{  
    [OperationContract]  
    int ReturnCount();  
}  
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]  
public class MathService: IMathService  
{  
    static int counter = 0;  
    public int ReturnCount()  
    {  
        counter++;  
        return counter;  
    }  
}  
Client  
public static void Main(string[] args)  
{  
    Console.WriteLine("Service Instance mode: Per Session ");  
    Console.WriteLine("Client making call to service...");  
    MyMathServiceServiceProxy.MyServiceProxy proxy = new MyMathServiceProxy.MyServiceProxy();  
    Console.WriteLine("Count: " + proxy.ReturnCount());  
    Console.WriteLine("Count: " + proxy.ReturnCount());  
    Console.WriteLine("Count: " + proxy.ReturnCount());  
    Console.WriteLine("Count: " + proxy.ReturnCount());  
    Console.ReadLine();  
}  

WCF Per Session

Single Instance Mode

In this mode, only single instance is going to create. It means to say, when you configured your service as Single Instance and client call the service then only and only single instance is created for the connection with client application. Instance is created when you host the service and dispose when you shut down the service.

So, if multiple clients access the same service at the same time then the only one instance will share the service data with multiple clients.

WCF Single Instance

[ServiceContract()]  
public interface IMathService  
{  
    [OperationContract]  
    int ReturnCount();  
}  
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]  
public class MathService: IMathService  
{  
    static int counter = 0;  
    public int ReturnCount()  
    {  
        counter++;  
        return counter;  
    }  
}  

Client

public static void Main(string[] args)  
{  
    Console.WriteLine("Service Instance mode: Single");  
    Console.WriteLine("Client making call to service...");  
    //Client A  
    MyMathServiceServiceProxy.MyServiceProxy proxy = new MyMathServiceProxy.MyServiceProxy();  
    Console.WriteLine("Count: " + proxy.ReturnCount());  
    Console.WriteLine("Count: " + proxy.ReturnCount());  
    Console.WriteLine("Count: " + proxy.ReturnCount());  
    Console.WriteLine("Count: " + proxy.ReturnCount());  
    Console.ReadLine();  
    //Client B  
    MyMathServiceServiceProxy.MyServiceProxy proxy2 = new MyMathServiceProxy.MyServiceProxy();  
    Console.WriteLine("Count: " + proxy2.ReturnCount());  
    Console.WriteLine("Count: " + proxy2.ReturnCount());  
    Console.WriteLine("Count: " + proxy2.ReturnCount());  
    Console.WriteLine("Count: " + proxy2.ReturnCount());  
    Console.ReadLine();  
}  

Thanks for reading this article, hope you enjoyed it.