WCF framework provides support to handle session in WCF service. You can use “SessionMode” attribute to maintain the session in WCF service. This attribute can be set while
defining the service contract. There are following values for this attribute.
1. SessionMode.Allowed
2. SessionMode.NotAllowed
3. SessionMode.Required
SessionMode.Allowed - It means Session is allowed but is not mandatory.
[ServiceContract(SessionMode = SessionMode.Allowed)]
SessionMode.NotAllowed - It means Session is not allowed for service contract.
[ServiceContract(SessionMode = SessionMode.NotAllowed )]
SessionMode.Required- It means Session is required for service contract.
[ServiceContract(SessionMode = SessionMode.Required)]
Now you can use following methods to control the WCF service instances-
1. PerCall
2. PerSession
3. Single (Singleton)
You have to specify any of these methods, when you are implementing the service contract.
PerCall- If you configured WCF service as “PerCall” then for each client request new service
instance will be created. For this implementation you have to set InstanceContextMode
attribute like this-
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
PerSession- If WCF service is configured as “PerSession” then for each client one service instance
will be created and same service instance will deal with all the requests of
current client. This is like “[Session]” object in ASP.Net. For this implementation
you have to set InstanceContextMode attribute like this-
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
Single(Singleton) - If WCF service is configured as “Single” then for all the clients only one service
instance will be created and this service instance will common for all the clients.
This is like “[Application]” object in ASP.Net. For this implementation you have
to set InstanceContextMode attribute like this-
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single )]
Service Contract (ISessionService.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WcfSession
{
//Service Contract for Percall
[ServiceContract(SessionMode = SessionMode.Required)]
public interface ISessionService_PerCall
{
[OperationContract]
int GetData_PerCall();
}
//Service Contract for Persession
[ServiceContract(SessionMode = SessionMode.Required)]
public interface ISessionService_PerSession
{
[OperationContract]
int GetData_PerSession();
}
//Service Contract for Singleton
[ServiceContract(SessionMode = SessionMode.Required)]
public interface ISessionService_Singleton
{
[OperationContract]
int GetData_Singleton();
}
}
Implementation of Service Contract
(SessionService.cs)
Here I have implemented service contracts, to increment the value by 1 for each call
but based on service instance implementation you will get different values.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WcfSession
{
//Implementing service contract for Percall
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class SessionService_PerCall : ISessionService_PerCall
{
public int value;
public int GetData_PerCall()
{
value = value + 1;
return value;
}
}
//Implementing service contract for Persession
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class SessionService_PerSession : ISessionService_PerSession
{
public int value;
public int GetData_PerSession()
{
value = value + 1;
return value;
}
}
//Implementing service contract for Singleton
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single )]
public class SessionService_Singleton : ISessionService_Singleton
{
public int value;
public int GetData_Singleton()
{
value = value + 1;
return value;
}
}
}
Configuration for WCF Service
App.Config File
Here I have set different configuration for each implementation.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<compilation debug="true" />
</system.web>
<system.serviceModel>
<services>
<!--Configuration for PerCall-->
<service name="WcfSession.SessionService_PerCall">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/WcfSession/SessionService_PerCall/" />
</baseAddresses>
</host>
<endpoint address="PerCall" binding="wsHttpBinding" contract="WcfSession.ISessionService_PerCall">
</endpoint>
</service>
<!--Configuration for PerSession-->
<service name="WcfSession.SessionService_PerSession">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/WcfSession/SessionService_PerSession/" />
</baseAddresses>
</host>
<endpoint address="PerSession" binding="wsHttpBinding" contract="WcfSession.ISessionService_PerSession">
</endpoint>
</service>
<!--Configuration for Singleton-->
<service name="WcfSession.SessionService_Singleton">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/WcfSession/SessionService_Singleton/" />
</baseAddresses>
</host>
<endpoint address="Singleton" binding="wsHttpBinding" contract="WcfSession.ISessionService_Singleton">
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
In above example, I have created three different endpoints-
http://localhost:8732/WcfSession/SessionService_PerCall/
http://localhost:8732/WcfSession/SessionService_PerSession/
http://localhost:8732/WcfSession/SessionService_Singleton/
Consuming service
To test these services, I have created one client application, here I have added
the reference of these services. Check the below code for client application.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PerCall_ServiceRef;
using PerSession_ServiceRef;
using Singleton_ServiceRef;
public partial class frmPerSession : System.Web.UI.Page
{
protected void btnPerCall_Click(object sender, EventArgs e)
{
try
{
//Creating object for SessionService_PerCallClient class
SessionService_PerCallClient objPerCall = new SessionService_PerCallClient();
//Call1
lblPerCall.Text = lblPerCall.Text + objPerCall.GetData_PerCall().ToString()
+ ", ";
//Call2
lblPerCall.Text = lblPerCall.Text + objPerCall.GetData_PerCall().ToString()
+ ", ";
//Call3
lblPerCall.Text = lblPerCall.Text + objPerCall.GetData_PerCall().ToString();
}
catch (Exception ex)
{
Response.Write("Unable to access WCF service. Details-" + ex.Message);
}
}
protected void btnPerSession_Click(object sender, EventArgs e)
{
try
{
//Creating object for SessionService_PerSessionClient class
SessionService_PerSessionClient objPerSession = new SessionService_PerSessionClient();
//Call1
lblPerSession.Text = lblPerSession.Text + objPerSession.GetData_PerSession().ToString()
+ ", ";
//Call2
lblPerSession.Text = lblPerSession.Text + objPerSession.GetData_PerSession().ToString()
+ ", ";
//Call3
lblPerSession.Text = lblPerSession.Text + objPerSession.GetData_PerSession().ToString();
}
catch (Exception ex)
{
Response.Write("Unable to access WCF service. Details-" + ex.Message);
}
}
protected void btnSingleton_Click(object sender, EventArgs e)
{
try
{
//Creating object for SessionService_SingletonClient class
SessionService_SingletonClient objSingleton = new SessionService_SingletonClient();
//Call1
lblSingleton.Text = lblSingleton.Text + objSingleton.GetData_Singleton().ToString()
+ ", ";
//Call2
lblSingleton.Text = lblSingleton.Text + objSingleton.GetData_Singleton().ToString()
+ ", ";
//Call3
lblSingleton.Text = lblSingleton.Text + objSingleton.GetData_Singleton().ToString();
}
catch (Exception ex)
{
Response.Write("Unable to access WCF. Details-" + ex.Message);
}
}
}
I have written code to test each implementation, just execute the code you will get
output-
Browser -1

Browser -2

PerCall- For this implementation every time we are getting value-1, 1, 1. It means that for each client request service instance is created.
PerCall- For this implementation we are getting incremented value- 1, 2, 3 (for Browser-1 and Browser-2). It means for each client it is maintaining different service instance.
Singleton- For this implementation we are getting incremented value- 1,2,3 (for Browser-1) and 4,5,6 (forBrowser-2). It means for all clients it is maintaining single service instance.
Now you are done. You can download the complete code from here-
WCFSessionService
WCFSessionClient