Handling Sessions in Silverlight
In this article, we are going to see ‘How to handle Sessions in Silverlight’.
Implementing Session Management:
Session is server side concept whereas the Silverlight page, first downloads the XAP file to the client’s PC and then runs from there. Hence, your Silverlight application can’t maintain the session. So, What to do?
You can easily maintain a application level static variable in the client side and store your data. Whenever you need, you can access from various Silverlight page across your application.
Here I will show how to do this. Create a static class called ‘SessionManager‘. Now create a Dictionary Property of type ‘string, object’ and name it as ‘Session’.
public class SessionManager { private static Dictionary<string, object> session = new Dictionary<string, object>; public static Dictionary<string, object> Session { get { return session; } set { session = value; } } }
Now from your application whenever you want to set a value to that object, use the SessionManager class to set the
key/value pair to the Session property.
SessionManager.Session["USR_Name"] = "Jayaraj";
When you want to access the same key/value, get it from the SessionManager as like below:
TxtUserName.Text = SessionManager.Session["USR_Name"].ToString();
That’s it. Now you are able to store values like you do in ASP.Net Session. But be sure that, it is application level and hence if you refresh the browser, you will loose all your records.