Click here to Skip to main content
Licence CPOL
First Posted 20 Jan 2009
Views 52,244
Downloads 1,600
Bookmarked 34 times

Access Sessions from Silverlight and ASP.NET

By A. Rajesh Kumar | 20 Jan 2009
Add or Retrieve Session Object from Silverlight and ASP.NET

1
1 vote, 5.9%
2
1 vote, 5.9%
3
7 votes, 41.2%
4
8 votes, 47.1%
5
4.50/5 - 17 votes
1 removed
μ 4.42, σa 1.50 [?]

Introduction

Another popular product from Microsoft is “Silverlight” for Rich Internet Applications (RIA). Its main intention is to provide more user interactive features with very good look & feel.

As Silverlight is targeting cross browser, cross platform applications, developers coming with Web Application experience compare different features that other web applications provide.

Most of the web applications require maintaining session state with the help of various mechanisms. While working with Silverlight, we may need Add or Get data from ASP.NET session.
As Silverlight works in client’s environment, it does not have direct access to ASP.NET session object. The only way we can retrieve the information from Session is through Web Service.

This article just explains how to work with this scenario with a practical working solution.

Assumptions

I assume that the developer who is ready to work with this example has little or more experience in working in ASP.NET, C#, WCF and Silverlight.

Development Tools

  • Visual Studio 2008
  • Silverlight SDK RTM 2.0

Let’s Start

Follow the steps given below to create the solution:

  1. Create a new Silverlight Web site from Visual Studio.
  2. Add a WCF Service to the Web application. We need it for Silverlight to Add or Get values from Session. By default, wsHTTP protocol is added to the configuration.
  3. Make sure that WCF is configured to “basicHTTP” protocol by changing the settings in web application’s “Web.config” file.
  4. If the WCF service comes with the Interface, do not hesitate to remove from the project as we are hosting it with the web application itself.
  5. Add the web service reference to the Silverlight application.
  6. Add 2 new web pages to the web application to work with Silverlight and web application Session state at the same time for our testing purposes.

Once you perform the above steps, your Visual Studio may look like this:

coolsource1.JPG

Each web page (ASP.NET page and Silverlight page) allows us to add and/or information from Session. The UI may look like this: 

coolsource2.JPG

coolsource2.JPG

In this example, we perform 4 different actions as given below:

  1. Add information to Session from ASP.NET page (ASPNetPage.aspx)
  2. Retrieve information from Session from ASP.NET page (ASPNetPage.aspx)
  3. Add information to Session from Silverlight page through WCF service(SilverlightPage.aspx)
  4. Retrieve information from Session from Silverlight page through WCF service(SilverlightPage.aspx)

In ASP.NET, we use the below code to store or retrieve the information in Session:

Session[variableName] = someValue; // Add or update information in Session
Object sessionVar = Session[variableName]; // Retrieve information in Session

The below code lets you add the information to Session from Silverlight:

public void btnAddToSession_Click(object sender, RoutedEventArgs e)
{
     lblAddToSessionStatus.Text = "Adding value to Session...";
     client.SetSessionValueForKeyAsync(txtAddSessionKey.Text, txtAddSessionValue.Text);
}
void client_SetSessionValueForKeyCompleted
	(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
      if ((null != e.Error) && (!String.IsNullOrEmpty(e.Error.Message)))
      {
          lblAddToSessionStatus.Text = "Error: " + e.Error.Message;
      }
      else
      {
          lblAddToSessionStatus.Text = "Successfully added.";
      }
}

The below code lets you retrieve the information in Session from ASP.NET page:

protected void btnGetFromSession_Click(object sender, EventArgs e)
{
     if (null != Session[txtGetFromSessionKey.Text])
     {
         txtGetFromSessionValue.Text = Session[txtGetFromSessionKey.Text] as string;
         lblGetFromSessionStatus.Text = "Done.";
     }
     else
     {
         lblGetFromSessionStatus.Text = "Session Key does not exist.";
     }
}

When you run the attached application, the UIs look like this:

coolsource4.JPG

coolsource4.JPG

Conclusion 

That's all for this part of Beginning Silverlight & ASP.NET sessions. I explained how Session values can be added/retrieved to Session Objects from Silverlight and ASP.NET. Though there may be many other ways to do the same job, I tried to give the basic implementation. Please feel free to give feedback.

I hope this article was helpful for you. :)

History

  • 20th January, 2009: Initial post

License

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

About the Author

A. Rajesh Kumar

Software Developer (Senior)

India India

Member
I am currently working as a Software Development Advisor. Practicing Object Oriented Design Patterns and flexible to learn new technologies.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 PinmemberChris Schnick19:39 11 Jan '12  
QuestionMultiple Values and Complex Objects - Issues PinmemberChris Schnick10:15 11 Jan '12  
AnswerRe: Multiple Values and Complex Objects - Issues PinmemberA. Rajesh Kumar19:14 11 Jan '12  
GeneralRe: Multiple Values and Complex Objects - Issues PinmemberChris Schnick19:30 11 Jan '12  
GeneralRe: Multiple Values and Complex Objects - Issues PinmemberA. Rajesh Kumar19:32 11 Jan '12  
QuestionNeed your help Mr. A. Rajesh Kumar Pinmembernbeiruty3:44 21 Dec '11  
AnswerRe: Need your help Mr. A. Rajesh Kumar PinmemberA. Rajesh Kumar4:20 21 Dec '11  
QuestionSilverlight Security Error when trying to access a WCF service PinmemberDennis Geasan17:36 27 Oct '11  
AnswerRe: Silverlight Security Error when trying to access a WCF service PinmemberDennis Geasan17:44 27 Oct '11  
GeneralRe: Silverlight Security Error when trying to access a WCF service PinmemberA. Rajesh Kumar20:20 27 Oct '11  
Generalnice example..! PinmemberDavid Castro8:31 13 Jun '11  
GeneralQuestion PinmemberVuyiswa Maseko20:25 9 Jun '11  
GeneralI'm keep getting this : "Object reference not set to an instance of an object." [modified] Pinmemberidanb6:54 5 Jun '11  
GeneralRe: I'm keep getting this : "Object reference not set to an instance of an object." PinmemberA. Rajesh Kumar0:20 7 Jun '11  
GeneralRe: I'm keep getting this : "Object reference not set to an instance of an object." PinmemberDavid Castro8:30 13 Jun '11  
GeneralMy vote of 4 Pinmemberbabsie4u19:22 19 Dec '10  
GeneralThanks! Pinmemberjohnzered1:42 12 Nov '10  
GeneralRe: Thanks! PinmemberA. Rajesh Kumar2:03 12 Nov '10  
QuestionAccess Sessions with JSon data from Silverlight and ASP.NET PinmemberChandra Vedula18:58 22 Sep '10  
QuestionAccess Sessions from Silverlight 4 by WCF service PinmemberMisicSlavisa22:41 30 Jun '10  
AnswerRe: Access Sessions from Silverlight 4 by WCF service PinmemberDiing5:01 26 Aug '10  
GeneralRe: Access Sessions from Silverlight 4 by WCF service PinmemberVuyiswa Maseko2:29 11 Jun '11  
GeneralRe: Access Sessions from Silverlight 4 by WCF service PinmemberA. Rajesh Kumar20:34 12 Jun '11  
GeneralRe: Access Sessions from Silverlight 4 by WCF service PinmemberVuyiswa Maseko1:51 13 Jun '11  
GeneralRe: Access Sessions from Silverlight 4 by WCF service PinmemberA. Rajesh Kumar1:58 13 Jun '11  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120209.1 | Last Updated 20 Jan 2009
Article Copyright 2009 by A. Rajesh Kumar
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid