Session Management in WCF and Silverlight

This article is about Session Management in WCF and Silverlight. This article will explain you the code and settings required in different files like web.config, service class, service interface etc.

Objective
 
This article is about "Session Management in WCF and Silverlight". 
 
Notecode and settings in files are written for silverlight.
 
Problems
 
I came across the situation to maintain a session in WCF for Silverlight and searched lot on web.
 
There are many ways to do it but none of them worked when the application is published and used across more than one silverlight clients.
 
For example - One simple way is use "persession" behavior of service in WCF that will manage session, but problem is that , persession requires "wshttpbinding" and silverlight does not support it wshttpbinding. Silverlight support basicHttpBinding and which not allow persession behavior in WCF. So solution for this problem is to use CustomBinding in web.config which works fine at both the side(WCF and Silverlight).
 
So here is way to do so..
 
Explanation
 
There are many complications for session in WCF and Silverlight,
 
For example - One simple way is use "persession" behavior of service in WCF that will manage session, but problem is that , persession requires "wshttpbinding" and silverlight does not support it wshttpbinding.silverlight support basicHttpBinding and which not allow persession behavior in WCF. So solution for this problem is to use CustomBinding in web.config which works fine at both the side(WCF and Silverlight).
 
Here is code
 
In Interface Iservice.cs
  1. [ServiceContract]  
  2. public interface IService1  
  3. {  
  4.     [OperationContract]  
  5.     void SetSession(string value);  
  6. }  
This is service interface in which I just declared prototype of service method which takes 1 parameter value.
 
Now lets see its implementation.
 
In Class Service.cs
  1. [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]  
  2. [AspNetCompatibilityRequirements(RequirementsMode =   AspNetCompatibilityRequirementsMode.Allowed)]   
  3. public class Service1 : IService1  
  4. {  
  5.       public void SetSession(string value)  
  6.     {  
  7.         HttpContext.Current.Session["Test"] = value;  
  8.     }  
  9. }  
See above is service class ,we first set Instance context mode to per session.Then in service method implementation we will set session value to the to session variable. We can give any name in the place of "Test" and set value dynamically when user login. In real scenario, we will set value as loginID of user and will use it throughout session of that user.
 
In Web.Config
  1. <behaviors>  
  2.       <serviceBehaviors>  
  3.         <behavior name="">  
  4.           <serviceMetadata httpGetEnabled="true" />  
  5.           <serviceDebug includeExceptionDetailInFaults="false" />  
  6.         </behavior>  
  7.       </serviceBehaviors>  
  8.     </behaviors>   
  9.     <bindings>  
  10.       <customBinding>  
  11.         <binding name="PortFolioWcfService.Service1.customBinding0">  
  12.           <binaryMessageEncoding />  
  13.           <httpTransport />  
  14.         </binding>  
  15.       </customBinding>  
  16.     </bindings>  
  17.     <serviceHostingEnvironment aspNetCompatibilityEnabled="true"  
  18.         multipleSiteBindingsEnabled="true" />   
  19.     <services>  
  20.       <service name="SimpleApp.Web.Service1">  
  21.         <endpoint address="" binding="customBinding" bindingConfiguration="PortFolioWcfService.Service1.customBinding0"  
  22.             contract="SimpleApp.Web.Service1" />  
  23.         <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />  
  24.       </service>  
  25.     </services>  
This code of web.config is very important for the successful execution of session. We should need to put "custombinding" explicitly as binding method in the clause like in Binding name and end point like above. So that's the code setting required for session now let's see how we can use it by example
 
Suppose, you want to execute user's login id to fetch the data from database based on current user.
 
So call above service setSession() and current user id as parameter it will create session for that user. Then while writing query user session variable as loginID because it having ID of user requesting query.
 
For example
  1. public String getProduct()  
  2. {  
  3.      Int32 SessionID=Convert.ToInt32(HttpContext.Current.Session["Test"])  
  4.      String Qry= "Select ProductName from UserProd where LoginID="+SessionID  
  5.      //Execute this query by connecting to Database  
  6. }  
So, above query will return different Product Name for different user as a resultset according to user who requested.
 
So, that's the one way of managing session in Silverlight and WCF,I am very glad to share this with you.