In the land of WCF service instance context management, PerSession services are the most used ones, probably because this mode is the default one in the communication foundation. In this setting, services (in default method usage scenarios) are created and destroyed as a reflection to the client proxy. This means, the transport layer and the service instance that is running on the server are both dedicated to the client side proxy. This model is sometimes referred to as “private session mode”.
The instance context mode as mentioned before, is set to PerSession by default. However, to explicitly declare this, one can use the ServiceBehaviour attribute to set this.
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
Class MyService: IServiceContract
However, the client that is using this service instance should be made somehow aware of the session mode usage. This can be also done through the service contract attribute.
[ServiceContract(SessionMode = SessionMode.Allowed)]
Again, this session mode is the default in the communication foundation, so under normal conditions, if you are implementing a “sessionful “ service, you need not worry about these settings. However, it is important to know the difference between different session modes.
“Allowed” session mode stands for possible PerSession implementation. However, PerCall services also comply with this setting on the contract.
“Required” session mode states that the service implementation is definitely a PerSession service and the client has to handle the transport layer session accordingly.
“NotAllowed” session mode is to deny any “sessionful” service implementation. In other words, with this service contract, there cannot exist a transport layer session; resulting in only PerCall service usage (even if the service is implemented with PerSession instance context management mode).
Another important factor for the handling of the session mode is the binding configuration. For instance, with basic http binding, there is no possible way to secure (in both reliability and security sense) the channel that manages the transport. For this reason, any session setting mentioned, will result in a PerCall service handling. However, WS binding with security and reliability can be used as the transport layer configuration.