जगदीश खोलिया: WCF Sessions

Thursday, January 31, 2013

WCF Sessions

WCF sessions are different than the session object in ASP.NET , support different behaviors, and are controlled in different ways.WCF sessions are very different from ASP.NET Sessions. In short,

ASP.NET Sessions are:
  1. Always server-initiated.
  2. Implicitly unordered.
  3. Provide a way to preserve data across multiple requests.There is unique session id generated at the server and passed back and forth between client and server via URL or cookies.
WCF Sessions are:
  1. Initiated and terminated by the calling application (WCF Client).
  2. Ordered message delivery.
  3. Sessions correlate a group of messages into a conversation.This correlation depdending upon the binding can be taken care at message or transport level.
  4. No data storage is involved with WCF Session.

To configure sessions in WCF, one should know the following three elements:

  1. Binding – Because all bindings do not support sessions. Only WS-*, NetTcpBinding and NetNamedPipeBinding have session support so selection of appropriate binding is necessary.
  2. SessionMode – This service contract specifies service’s possible expectation for session from incoming client request. It has three self describing values:
    *)Allowed – Service can accept sessionful clients as well as sessionless.
    *)Required – Service will entertain only those clients who have session, sessionless client cannot    connect to service.
    *)NotAllowed – Service can interact only with sessionless clients. Just opposite to Required.
  3. InstanceContextMode – This is a service behavior that controls instantiation of actual service class. It has the following values:
    *)PerCall – Each request made to server will be served by a new instance of service class.
    *)PerSession – A Session will have its dedicated service instance and all requests pertaining to the session will be served by that instance only. All sessions will have their individual dedicated service instances.
    *)Single –All requests to the service will be served by a single unique instance of service class.  
There are 3 things to remember for WCF session:
1) Sessionful binding. 
      (
          <endpoint address="http://localhost:750" binding="wsHttpBinding" 
           contract="wcfservice.Iservice" name="wcfSessionTest"/>
       )
2) SessionMode service contract.
   ( 
      [ServiceContract(SessionMode = SessionMode.Allowed)]
      public interface Iservice 
      {
         //some code…
      }
   ) 
3) InstanceContextMode service behavior. 
      (
          [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
            public class serviceclass : Iservice
           { // some code… }
       )

Post a Comment