How to access ASP.NET session in Silverlight?

By Santhosh N

This explain how to access ASP.NET Session in Silverlight

To begin with Silverlight cannot access Session of ASP.NET as Silverlight is on client side and ASP.NET is server side and there is no direct communications between both.

Now, the question is how to access ASP.NET session state from Silverlight application. Here is an approach how to accomplish this, let us examine the steps to achieve this:

1) Create a service contract which exposes a session query method using WCF as we consume this from Silverlight as Silverlight cannot directly talk to ASP.NET.

[ServiceContract]
public interface ISessionService
{
[OperationContract]
string SpeakToASPNETSession(string str)
}

2) To implement this service contract, create a WCF service class applying AspNetCompatibilityRequirementsAttribute.

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed]
public class WcfServiceToSession: ISessionService
{
public string SpeakToASPNETSession(string str)
{
return HttpContext.Current.Session[str] as string;
}
}
3) Finally, we need to enable the aspNetCompatibility function in the Web.config by adding the following line:

<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />

Related FAQs

This explains if Silverlight supports DataSet and the reasons for supporting/ not supporting.
This explains how to troubleshoot the deployment issues of WCF access after deploying the Silverlight sites on IIS
This explains the difference between the different versions of the Silverlight
This explain how to allow Impersonation in ASP.NET Web Application.
This explain how to read the identity of the impersonated user in ASP.NET programatically.
This explain the page life cycle event that is best suited to set the Master Page for the webpage in ASP.NET programatically at runtime.
How to access ASP.NET session in Silverlight?  (1515 Views)