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.
  	- Per Call
  	- Per Session
  	- 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.
- [ServiceContract]  
 - public interface IInstatnceModeSample {  
 -     [OperationContract]  
 -     int Increament();  
 - }  
 
  Class File
 - [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]  
 - public class InstatnceModeSample: IInstatnceModeSample {  
 -     int a = 0;  
 -     public int Increament() {  
 -         a = a + 1;  
 -         return a;  
 -     }  
 - }  
 
  Hosting File
 - ServiceHost host = new ServiceHost(typeof(InstatnceModeSample.InstatnceModeSample));  
 - host.Open();  
 - Console.Write("Service Hosted");  
 - Console.Read();  
 
 Client File
 - ServiceReference1.InstatnceModeSampleClient clt = new ServiceReference1.InstatnceModeSampleClient();  
 - Console.WriteLine(clt.Increament().ToString());  
 - Console.WriteLine(clt.Increament().ToString());  
 - Console.WriteLine(clt.Increament().ToString());  
 - 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
 - [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]  
 - public class InstatnceModeSample: IInstatnceModeSample {  
 -     int a = 0;  
 -     public int Increament() {  
 -         a = a + 1;  
 -         return a;  
 -     }  
 - }  
 
 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
- [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]  
 - public class InstatnceModeSample: IInstatnceModeSample {  
 -     int a = 0;  
 -     public int Increament() {  
 -         a = a + 1;  
 -         return a;  
 -     }  
 - }  
 
 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.