Monday 28 December 2015

What are session modes in WCF? How can you make a service as sessionful?



ServiceContract attribute offers the property SessionMode which is used to specify the session mode. There are three session modes supported by WCF:


  • Session.Allowed(default): Transport sessions are allowed, but not enforced. Service will behave as a per-session service only if the binding used maintains a transport-level session.
  • Session.Required: Mandates the use of a transport-level session, but not necessarily an application-level session.
  • Session.NotAllowed: Disallows the use of a transport-level session, which precludes an application-level session. Regardless of the service configuration, the service will always behave as a per-call service.




[ServiceContract(SessionMode = SessionMode.NotAllowed)]
interface IMyContract
{.................}

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
class MyService : IMyContract
{.................}








In this above post I explained you can limit how many instances or sessions are created at the application level

Can you limit how many instances or sessions are created at the application level?


Yes, you can limit how many instances or sessions are created at the application level. For this, you need to configure throttling behavior for the service in its configuration file. Some of these important properties are:



  • maxConcurrentCalls limits the total number of calls that can currently be in progress across all service instances. The default is 16.
  • maxConcurrentInstances limits the number of InstanceContext objects that execute at one time across a ServiceHost. The default is Int32.MaxValue.
  • maxConcurrentSessions limits the number of sessions a ServiceHost object can accept. It is a positive integer that is 10 by default.






<behaviors>
 <serviceBehaviors>
   <behavior name="ServiceBehavior">
     <serviceThrottling maxConcurrentCalls="500" maxConcurrentInstances ="100" maxConcurrentSessions ="200"/>
   </behavior>





In this above post I explained you can limit how many instances or sessions are created at the application level