Objective
This article is about "Session Management in WCF and Silverlight".
Note - code 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
- [ServiceContract]
- public interface IService1
- {
- [OperationContract]
- void SetSession(string value);
- }
Now lets see its implementation.
In Class Service.cs
- [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
- [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
- public class Service1 : IService1
- {
- public void SetSession(string value)
- {
- HttpContext.Current.Session["Test"] = value;
- }
- }
In Web.Config
- <behaviors>
- <serviceBehaviors>
- <behavior name="">
- <serviceMetadata httpGetEnabled="true" />
- <serviceDebug includeExceptionDetailInFaults="false" />
- </behavior>
- </serviceBehaviors>
- </behaviors>
- <bindings>
- <customBinding>
- <binding name="PortFolioWcfService.Service1.customBinding0">
- <binaryMessageEncoding />
- <httpTransport />
- </binding>
- </customBinding>
- </bindings>
- <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
- multipleSiteBindingsEnabled="true" />
- <services>
- <service name="SimpleApp.Web.Service1">
- <endpoint address="" binding="customBinding" bindingConfiguration="PortFolioWcfService.Service1.customBinding0"
- contract="SimpleApp.Web.Service1" />
- <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
- </service>
- </services>
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
- public String getProduct()
- {
- Int32 SessionID=Convert.ToInt32(HttpContext.Current.Session["Test"])
- String Qry= "Select ProductName from UserProd where LoginID="+SessionID
- //Execute this query by connecting to Database
- }
So, that's the one way of managing session in Silverlight and WCF,I am
very glad to share this with you.

Nirmal NagarajanPosted Aug 11, 2013, 1:47 AM
how i download this article...!!!
Balaji SelvarajanPosted Aug 28, 2012, 9:02 AM
Good
Akshay DalviPosted Nov 10, 2011, 12:09 AM
Hi Pravin, I found this useful in my project. Keep posting..