I have a session variable that I want to access in a Silverlight 3 application. How can I do it in the best recommended way?
If it is a mutable value, i.e. changes after the session initialization, use an Ajax or WCF call to a service endpoint to get/set the value. The ScriptService or 'Ajax Enabled WCF' endpoint will both have access to the session.
If the value is immutable as the hosting page is being rendered, you could, and I don't suggest this, write it out as a JSON object to the hosting page.
In the object tag in your Silverlight aspx host page:
<param name="initParams" value="myvar=<%=Session["myvar"] %>" />
then retrieve the value in app.xaml.cs
private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new Page();
myvar = e.InitParams["myvar"];
}
Not the most secure way and as has been noted only work best for immutable session variables.
The other way which was already suggested was to expose the session variable using a WCF service. It all depends on how you want to retrieve the value, how secure you want the process to be, which of serveral ways you want to access the value and from where(in the web page or in the Silverlight app). I hope some of this makes some sense.