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);
- }
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
- [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
- [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
- public class Service1 : IService1
- {
- public void SetSession(string value)
- {
- HttpContext.Current.Session["Test"] = value;
- }
- }
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
- <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>
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
- public String getProduct()
- {
- Int32 SessionID=Convert.ToInt32(HttpContext.Current.Session["Test"])
- String Qry= "Select ProductName from UserProd where LoginID="+SessionID
-
- }
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.