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:
- Create a new Silverlight Web site from Visual Studio.
- 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.
- Make sure that WCF is configured to “
basicHTTP
” protocol by changing the settings in web application’s “Web.config” file.
- 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.
- Add the web service reference to the Silverlight application.
- 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:
Each web page (ASP.NET page and Silverlight page) allows us to add and/or information from Session
. The UI may look like this:
In this example, we perform 4 different actions as given below:
- Add information to
Session
from ASP.NET page (ASPNetPage.aspx)
- Retrieve information from
Session
from ASP.NET page (ASPNetPage.aspx)
- Add information to
Session
from Silverlight page through WCF service(SilverlightPage.aspx)
- 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; Object sessionVar = Session[variableName];
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:
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