Sessions in WCF refers controlling correlation of messages between two end points.
Session Modes
Instance Context Modes
Session Modes
- Allowed: Clients can connect with or without establishing session
- Required: Client can connect only with establishing session
- NotAllowed: Client can connect only without session
[ServiceContract(SessionMode= SessionMode.Required)]
public interface IService1
{
[OperationContract]
string GetData();
}
Session is not supported by all bindings. Only WS-*, netTcpBinding & netNamedPipe are support wcf sessions.
If we use bindings other than mentioned above, service throws exception
Instance Context Modes
- PerCall: Each request made to the service is served by a new instance of service
- PerSession: All requests made to the service served by a single service instance for a particular single session
- Single: All request made to the service served by a single service instance even from different clients
[ServiceBehavior(InstanceContextMode= InstanceContextMode.PerSession)]
public class Service1 : IService1
{
int counter = 0;
public string GetData()
{
return string.Format("Counter : {0} , Session Id: {1}", counter++, OperationContext.Current.SessionId);
}
}











