Instance Mode In WCF

Hello everyone. Today, in this article, we will show the Instance Mode in WCF Service. Instance means an object of the Service being created by the client for accessing WCF Service methods. There are generally 3 types of Instance Modes in WCF.

  1. Per Call
  2. Per Session
  3. Single

Per Call

In Per Call Instance Mode, when the client calls the Service, a new instance (object) of the Service is created every time. Whether the client is the same or different, we can say that every time; the client will create a new instance. See the example given below for better understanding.

Example

Create WCF Service and create method in an interface file where method name is "Increment" and its return type is an integer. Now, implement this method on class, as shown below, and also declare global integer variable "a".

NOTE

I am not going to go into details about creating the Service because we already learned it in a previous article, so if you want to learn about creating and hosting WCF Service, go to the article given below.

  1. [ServiceContract]  
  2. public interface IInstatnceModeSample {  
  3.     [OperationContract]  
  4.     int Increament();  
  5. }  
Class File
  1. [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]  
  2. public class InstatnceModeSample: IInstatnceModeSample {  
  3.     int a = 0;  
  4.     public int Increament() {  
  5.         a = a + 1;  
  6.         return a;  
  7.     }  
  8. }  
Hosting File
  1. ServiceHost host = new ServiceHost(typeof(InstatnceModeSample.InstatnceModeSample));  
  2. host.Open();  
  3. Console.Write("Service Hosted");  
  4. Console.Read();  
Client File
  1. ServiceReference1.InstatnceModeSampleClient clt = new ServiceReference1.InstatnceModeSampleClient();  
  2. Console.WriteLine(clt.Increament().ToString());  
  3. Console.WriteLine(clt.Increament().ToString());  
  4. Console.WriteLine(clt.Increament().ToString());  
  5. Console.Read();  
Output



Per Session

In Per session Instance Mode, when the client calls the Service for the first time, one instance is created for this client and that is used during this current session for the particular client. After the session drops, the client has to create a new instance. For better understanding, see the example given below.

Now, Per Session mode example is same, as shown above. We have to change an Instance Mode to Per Session.

Class File
  1. [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]  
  2. public class InstatnceModeSample: IInstatnceModeSample {  
  3.     int a = 0;  
  4.     public int Increament() {  
  5.         a = a + 1;  
  6.         return a;  
  7.     }  
  8. }  
Now, build the Service and update the client service reference, then the output is shown.

OUTPUT



Single

During Single Instance Mode, only one instance of Service is created during the full communication among all the clients. Now, for Single Mode the example is similar, and as shown above, we have to change Instance Mode to Single.

Class File
  1. [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]  
  2. public class InstatnceModeSample: IInstatnceModeSample {  
  3.     int a = 0;  
  4.     public int Increament() {  
  5.         a = a + 1;  
  6.         return a;  
  7.     }  
  8. }  
Now, build the Service and update the client Service reference. Subsequently, run the client and show the output. Here, we run 2 clients at a time.

Output



Here, you can say that first client generates an output 1 to 3 (1,2,3) and the second client will generate output 4 to 6 (4,5,6), so we can say the second client will continue the number increment from the first client execution, so here only one instance is created among both the clients, so it calls Single Instance mode.

Up Next
    Ebook Download
    View all
    Learn
    View all