Session State in Windows Azure
We recently moved a web application into Windows Azure that was using session state. As it was running on a single webserver the session state was set to InProc but this is not useful when in a multi-server environment as the session is stored on the specific machine and is therefore not accessible to other machines. There were a number of options:
- Use the Windows AppFabric Caching service (http://msdn.microsoft.com/en-us/library/windowsazure/gg278339.aspx)
- Use SQL Azure (http://blogs.msdn.com/b/sqlazure/archive/2010/08/04/10046103.aspx)
- Use Windows Azure Storage
Windows Azure Storage seemed to be the more cost effective version as the site does not currently use SQL Azure and they have purchased a subscription for Azure which includes both transaction and storage costs.
There is a sample asp.net session provider that uses Windows Azure Table Storage as its backing store. The sample can be downloaded from MSDN at
http://code.msdn.microsoft.com/windowsazure/Windows-Azure-ASPNET-03d5dc14
How to use the Azure Storage Session State Provider
Add the following Session State provider config to the web.config file of the project
1 1: <!-- SessionState Provider Configuration -->
2: <sessionState mode="Custom"
1 3: customProvider\="TableStorageSessionStateProvider"\>
4: <providers>
1 5: <clear/>
6: <add name="TableStorageSessionStateProvider" type="Microsoft.Samples.ServiceHosting.AspProviders.TableStorageSessionStateProvider"/>
1 7: </providers\>
8: </sessionState>
1
2Add your windows azure storage connection string (DataConnectionString) to each web role that requires session state (Not setting this will result in an object reference not set to an instance of an object exception)
3
4Add a reference to the ASPProviders.dll taken from the sample project and make sure that the Copy Local property is set to true (Not setting this will cause an unable to load exception)
5
6[![image](/wp-content/uploads/sites/17/historic/image_thumb_36961302.png "image")](http://blogs.blackmarble.co.uk/blogs/sspencer/image_16E7393A.png)
7
8We also added a reference to System.DataServices.Client and set copy local to true on this too.(Not sure if this is needed)
9
10Once this is setup and running, add multiple instances to your role configuration and run in the debugger. Make sure you can navigate to the page that has the session data in. I put a break point onto the action of the page and added a watch for Microsoft.WindowsAzure.ServiceRuntime.RoleInstance.CurrentRoleInstance.Id and checked to see if it changed and if it did change checked to see if the session data was visible.
11
12You may well get the following error when you are using session as all the objects that are put into the Azure Table Storage session object need to be serializable.
13
14**_Unable to serialize the session state. In 'StateServer' and 'SQLServer' mode, ASP.NET will serialize the session state objects, and as a result non-serializable objects or MarshalByRef objects are not permitted. The same restriction applies if similar serialization is done by the custom session state store in 'Custom' mode._**
15
16You can check to see the session data in the Azure Storage Server Explorer.
17
18[![image](/wp-content/uploads/sites/17/historic/image_thumb_49D6C9A1.png "image")](http://blogs.blackmarble.co.uk/blogs/sspencer/image_3629E00D.png)
19
20We are going to run this for a while to se how well it works and also see what debris is left behind in the table and blob storage due to ended sessions. We might have to have a job running that tidies up the expired sessions later.