Click here to Skip to main content
15,879,239 members
Articles / Programming Languages / C#

Tips & Tricks: Session Management in Silverlight

Rate me:
Please Sign up or sign in to vote.
4.57/5 (7 votes)
16 Sep 2010CPOL1 min read 24.5K   7   4
Session Management in Silverlight

In various forums (mainly in Silverlight.Net Forum), I have seen people asking a question: “How to implement Session Management in Silverlight?” The simple answer is: “Session Management is not available in Silverlight”. Why? If it is not there, how can we implement it?

Ok, if you are new to Silverlight, you need to know about this. Hence, in this post, I will describe Silverlight Session Management. The targeted audience for this post is mainly beginners of Silverlight. Read it and if you have any queries, let me know.

As you all know that once you visit the Silverlight site, it first downloads the .XAP file to the client’s PC and then runs from there. Other side, the session is a server side concept. It maintains in the server. Hence, your Silverlight application can’t maintain the session. :( So, what to do?

You can easily maintain an application level static variable in the client side and store your data. Whenever you need, you can access from various Silverlight pages 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”.

Image 1

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.

Image 2

When you want to access the same key/value, get it from the SessionManager.Session property. Have a look into the code:

Image 3

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 lose all your records.

Image 5

Image 6

Image 7

This article was originally posted at http://www.kunal-chowdhury.com/feeds/posts/default

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
India India

Kunal Chowdhury is a former Microsoft "Windows Platform Development" MVP (Most Valuable Professional, 2010 - 2018), a Codeproject Mentor, Speaker in various Microsoft events, Author, passionate Blogger and a Senior Technical Lead by profession.

He is currently working in an MNC located in India. He has a very good skill over XAML, C#, Silverlight, Windows Phone, WPF and Windows app development. He posts his findings, articles, tutorials in his technical blog (www.kunal-chowdhury.com) and CodeProject.


Books authored:


Connect with Kunal on:





Comments and Discussions

 
GeneralMy vote of 3 Pin
Member 697352325-Jan-11 15:15
Member 697352325-Jan-11 15:15 
GeneralMy vote of 2 Pin
Dima Pasko26-Sep-10 8:29
Dima Pasko26-Sep-10 8:29 
GeneralMy vote of 5 Pin
Vimarsh N Sharma21-Sep-10 19:49
Vimarsh N Sharma21-Sep-10 19:49 
RantUse With Care!! Pin
InfRes20-Sep-10 22:46
InfRes20-Sep-10 22:46 
This is essentially a Global Variable, which is a bad thing!

Use it judiciously, and architect the solution properly in preference to using this.


Well written article, but caveats should also be included.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.