Introduction
The understanding of this article requires an understand of some basics
regarding WCF. Once again this will be an article for beginners in WCF to
understand instancing.
Context Modes are very confusing for developers to understand and use. Although
many articles exist in the web, I was thinking whether I should write one. I
decided to write this article in simpler language and more explanatory for
beginners to learn Instancing.
An Instance Context mode defines how long a service instance remains on the
server.
Request Response Cycle
Whenever the client sends a request to a WCF service, what exactly happens
behind the scenes? Basically after making a client request the WCF service will
create a service class instance at the service that will do the operations
involved and then it will return the response back to the client. In this
request and response process the service instance object has been created in the
process. Now the question arises, what will be the scope of that object or
lifetime of that instance?
Instance Context mode in WCF
Here comes the concept of Instance Context mode in WCF.
The WCF framework has defined the following three Instance Context modes:
PerCall: a new instance of the service will be created for the request from the
same client or a different client, meaning every request is a new request. In
this mode no state is maintained.
PerSession: a new Instance will be created for every new client session and the
scope of that object will be the scope of that session.
Single: a single instance will be created for the service object that will take
care of all the requests coming from the same client or a different one.
By decorating the service with a service behavior, an Instance Context mode can
be set.
- [ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall)]

PerCall Mode
A new instance of a service will be created for the request from the same client or a different client, meaning it will take every request as a new request.
Sample Code: Per Session Sample code with service, client and configuration.
Service Code
- IOrderService
- namespace CustomerOrderService
- {
- [ServiceContract]
- public interface IOrderService
- {
- [OperationContract]
- int GetCallCount();
- [OperationContract]
- Customer GetCusomersDetails(int ID);
- [OperationContract]
- void UpdateCustomers(int ID);
- [OperationContract]
- void AddCustomers(Customer cust);
- }
- }
- namespace CustomerOrderService
- {
- [ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall)]
- public class OrderService : IOrderService
- {
- Customer cust = null;
- private int _callCount;
- public int GetCallCount()
- {
- _callCount=_callCount+1;
- return _callCount;
- }
- public Customer GetCusomersDetails()
- {
- throw new NotImplementedException();
- }
- public void AddCustomers(Customer cust)
- {
- throw new NotImplementedException();
- }
- public Customer GetCusomersDetails(int Id)
- {
- throw new NotImplementedException();
- }
- public void UpdateCustomers(int ID)
- {
- throw new NotImplementedException();
- }
- }
- }
Client Code
- class Program {
- static void Main(string[] args) {
- int CallCount;
- OrderService.OrderServiceClient orderService = new OrderServiceClient();
- CallCount = orderService.GetCallCount();
- Console.WriteLine(CallCount);
- CallCount = orderService.GetCallCount();
- Console.WriteLine(CallCount);
- CallCount = orderService.GetCallCount();
- Console.WriteLine(CallCount);
- Console.ReadLine();
- }
- }
Output
The client has called the method 3 times giving 1 as the output every time from the same instance of the service but it will consider it as a new call although it is being called.
Scenario: When to use per call is very important to understand. Suppose there is the requirement to build a scalable application and you don't need state between requests then per call would be the good choice because the per call mode is single-threaded and the instance would be destroyed after each call.

GeeteshPosted Jan 18, 2019, 9:04 AM
PerSessionMode is not working for NetTcpBinding as well.
Mathew WilkinsPosted Jun 20, 2017, 3:48 AM
Excellent article, very clear and understandable. Thank you.
Sudheshwer RaiPosted Jun 10, 2017, 4:47 AM
Nice Abhishek, Keep it up.
NitinPosted Apr 13, 2015, 2:45 AM
good one
Former memberPosted Apr 8, 2015, 4:41 AM
Good one
manish tiwariPosted Apr 7, 2015, 5:59 AM
nice article sir..
Santhakumar MunuswamyPosted Apr 7, 2015, 5:09 AM
thanks for nice article
Yogendra YadavPosted Apr 7, 2015, 3:20 AM
good one...........
Gowtham RajamanickamPosted Apr 7, 2015, 12:26 AM
nice