Tips & Tricks: 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, I will describe you in this post about Silverlight Session Management. The targeted audience for this post is mainly the 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. Sad smile 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”.

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.

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

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.

If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Twitter, Facebook, Google+ and LinkedIn. I would love to hear your thoughts and opinions on my articles directly. Also, don't forget to share your views and/or feedback in the comment section below.

7 comments

  1. Hi Cgcamilo .. This is right..Silverlight application run only on client side (Calling services are different thing). To test applicatioon without services just publish on server and access from client machine..after loading page just remove client-server network connection. and u can see your application is running smoothly... Thanks --- Sachin Karche

    ReplyDelete
  2. You given very well code but how we manage session time

    ReplyDelete
  3. That you can't do simply. But if you need, you can try with the DispatcherTimer.

    ReplyDelete
  4. Was clear and effective for a beginner

    ReplyDelete
  5. then how can maintain user specific session their should be some solution i think now you can update it

    ReplyDelete
  6. Really a excellent post for beginners.

    ReplyDelete
  7. Thanks,

    thanks for the explanation, as you say the problem with variables comes when you dont realize that silverlight app is running on client side.

    Bye!

    ReplyDelete

 
© 2008-2016 Kunal-Chowdhury.com - Microsoft Technology Blog for developers and consumers | Designed by Kunal Chowdhury
Back to top