Different Ways To Create Instance Of The WCF Service

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.

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.

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

  1. [ServiceContract()]  
  2. public interface IMathService  
  3. {  
  4.     [OperationContract]  
  5.     int ReturnCount();  
  6. }  
  7. [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]  
  8. public class MathService: IMathService  
  9. {  
  10.     static int counter = 0;  
  11.     public int ReturnCount()  
  12.     {  
  13.         counter++;  
  14.         return counter;  
  15.     }  
  16. }  
  17. Client  
  18. public static void Main(string[] args)  
  19. {  
  20.     Console.WriteLine("Service Instance mode: Per Call ");  
  21.     Console.WriteLine("Client making call to service...");  
  22.     MyMathServiceServiceProxy.MyServiceProxy proxy = new MyMathServiceProxy.MyServiceProxy();  
  23.     Console.WriteLine("Count: " + proxy.ReturnCount());  
  24.     Console.WriteLine("Count: " + proxy.ReturnCount());  
  25.     Console.WriteLine("Count: " + proxy.ReturnCount());  
  26.     Console.WriteLine("Count: " + proxy.ReturnCount());  
  27.     Console.ReadLine();  
  28. }  
percall

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
  1. [ServiceContract()]  
  2. public interface IMathService  
  3. {  
  4.     [OperationContract]  
  5.     int ReturnCount();  
  6. }  
  7. [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]  
  8. public class MathService: IMathService  
  9. {  
  10.     static int counter = 0;  
  11.     public int ReturnCount()  
  12.     {  
  13.         counter++;  
  14.         return counter;  
  15.     }  
  16. }  
  17. Client  
  18. public static void Main(string[] args)  
  19. {  
  20.     Console.WriteLine("Service Instance mode: Per Session ");  
  21.     Console.WriteLine("Client making call to service...");  
  22.     MyMathServiceServiceProxy.MyServiceProxy proxy = new MyMathServiceProxy.MyServiceProxy();  
  23.     Console.WriteLine("Count: " + proxy.ReturnCount());  
  24.     Console.WriteLine("Count: " + proxy.ReturnCount());  
  25.     Console.WriteLine("Count: " + proxy.ReturnCount());  
  26.     Console.WriteLine("Count: " + proxy.ReturnCount());  
  27.     Console.ReadLine();  
  28. }  
Per Session Service

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 shutdown 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.

single instance mode
  1. [ServiceContract()]  
  2. public interface IMathService  
  3. {  
  4.     [OperationContract]  
  5.     int ReturnCount();  
  6. }  
  7. [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]  
  8. public class MathService: IMathService  
  9. {  
  10.     static int counter = 0;  
  11.     public int ReturnCount()  
  12.     {  
  13.         counter++;  
  14.         return counter;  
  15.     }  
  16. }  
  17. Client  
  18. public static void Main(string[] args)  
  19. {  
  20.     Console.WriteLine("Service Instance mode: Single");  
  21.     Console.WriteLine("Client making call to service...");  
  22.     //Client A  
  23.     MyMathServiceServiceProxy.MyServiceProxy proxy = new MyMathServiceProxy.MyServiceProxy();  
  24.     Console.WriteLine("Count: " + proxy.ReturnCount());  
  25.     Console.WriteLine("Count: " + proxy.ReturnCount());  
  26.     Console.WriteLine("Count: " + proxy.ReturnCount());  
  27.     Console.WriteLine("Count: " + proxy.ReturnCount());  
  28.     Console.ReadLine();  
  29.     //Client B  
  30.     MyMathServiceServiceProxy.MyServiceProxy proxy2 = new MyMathServiceProxy.MyServiceProxy();  
  31.     Console.WriteLine("Count: " + proxy2.ReturnCount());  
  32.     Console.WriteLine("Count: " + proxy2.ReturnCount());  
  33.     Console.WriteLine("Count: " + proxy2.ReturnCount());  
  34.     Console.WriteLine("Count: " + proxy2.ReturnCount());  
  35.     Console.ReadLine();  
  36. }  
Thanks for reading this article, hope you enjoyed it.


Similar Articles