Tuesday 11 August 2009

Silverlight 3 and Sessions

Silverlight does NOT support Session object as you may know in ASP/ASP.NET. The reason is because the Silverlight runs on the Client.

In most cases you will not need Session object at all. If you browse over the Internet you'll probably find some solutions how to implement Session object to your Silverlight application via WCF Service. The main idea of this approach is that your Silverlight application call function from the WCF service to set "something" to the Session object and another call to the WCF service is required to get value from the Session object.

Happily, Silverlight has something like Session object - Isolated storage. It's used for storing application settings. It can contain various types of information, such as text, XML, and serialized objects. The default space limit is a mere 1MB (although you can request that the user grant you more). Isolated storage is persistent—unlike the browser cache, it never expires and it’s not removed if the user chooses to explicitly delete temporary Internet files. The user can manually delete isolated storage area (see folder location below) or you can delete it in your code by calling the Remove or Clear method.

The base location of the isolated storage area on each operating system supported in Silverlight 2+:

Mac OS XAppData\Local
Windows XPC:\Documents and Settings\[UserName]\Application Data\Microsoft\Silverlight\is
Windows Vista/Windows 7C:\Users\[UserName]\AppData\LocalLow\Microsoft\Silverlight\is

To use the Isolated storage you need to import the namespace to your Silverlight application:
Imports System.IO.IsolatedStorage.IsolatedStorageSettings

When you call Save method (e.g. ApplicationSetting.Save()) data stored in ApplicationSettings are saved to above location on Client's HDD. Beware: Saved data are not encrypted - if you need to store sensitive data you have to encrypt them - see example of encryption utility here.

This namespace contains two settings option:
  1. ApplicationSetting - ApplicationSettings are stored as per-application, per-computer, and per-user settings. Their scope is determined by the full path of the application .xap file.
  2. SiteSetting - SiteSettings are stored as per-domain, per-computer, and per-user settings. Their scope is determined by the sub-domain that hosts the application .xap file.
Example:
Suppose, you'd like to store a username from a login form. There's a text block named txtUserName and an OK button named btnOK on the login form. To display stored value you have another text block named txtStoredName and a button named btnShow that you'll use to
display stored value. You want to store the username on the application level.

Imports System.IO.IsolatedStorage.IsolatedStorageSettings
Private Sub btnOK_Click(ByVal sender As System.Object, _
ByVal e As System.Windows.RoutedEventArgs)
ApplicationSetting.Add("userName", txtUserName.Text)
ApplicationSetting.Save() 'saves stored data to disk
End Sub

Private Sub btnShow_Click(ByVal sender As System.Object, _
ByVal e As System.Windows.RoutedEventArgs)
If ApplicationSetting.Contains("userName") Then
txtStoredName.Text = ApplicationSetting("userName").ToString
Else
txtStoredName.Text = "Sorry, nothing stored"
End If
End Sub

You can for example store in the Isolated Storage file, just add this code:

Imports System.IO
Imports System.IO.IsolatedStorage
Private Sub CreateAndWriteToFile()
Using store As IsolatedStorageFile = _
IsolatedStorageFile.GetUserStoreForApplication
Using file As IsolatedStorageFileStream = _
store.CreateFile("sample.txt")
file.Close()
End Using
Using sw As New StreamWriter(store.OpenFile("sample.txt", _
FileMode.Open, FileAccess.Write))
sw.WriteLine("Test Text")
End Using
End Using
End Sub

Private Sub ReadFromFile()
Dim result As String
Using store As IsolatedStorageFile = _
IsolatedStorageFile.GetUserStoreForApplication
Using sr As New StreamReader(store.OpenFile("sample.txt", _
FileMode.Open))
result = sr.ReadToEnd
End Using
End Using
End Sub

How to increase Isolated Storage size

When you right click anywhere inside your Silverlight application displayed on the browser, a pop-up named "Microsoft Silverlight Configuration" will appear. Click "Application Storage" tab to get information about storage size. To increase the size you need to write a code to request a user to grant it.
So, it can be done as follows. Suppose, you need 6MB of storage size:

Imports System.IO.IsolatedStorage
Using store As IsolatedStorageFile = _
IsolatedStorageFile.GetUserStoreForApplication
Dim spaceNeeded As Int64 = 1048576 * 6
Dim spaceAvailable As Int64 = store.AvailableFreeSpace
If spaceNeeded > spaceAvailable Then
If store.IncreaseQuotaTo(spaceNeeded) Then
' user granted increase, the store is now 6MB
' put your code here
End If
End If
End Using

No comments: