Being able to store session data is arguably one of the reasons that the Web features the dynamic applications we all use every day. If it wasn’t for the session we would have a fairly boring static Web experience.
Windows Azure, being built on top of Windows Server 2008, allows ASP.Net developers to utilise all the session features of IIS. Additionally the developer can extend these options with the Windows Azure table storage to cheaply and easily persist storage of session data in the Cloud.
Session storage for Windows Azure
The scalability of Windows Azure means that you are going to have at least a couple of web roles and possibly even tens or hundreds of web roles for busy sites. All these individual web roles need access to a central session store, the best place to put each user’s session data is in Windows Azure table storage.
The process of getting started with storing session data in the table store requires the modification of the following files.
As per normal ASP.Net web.config settings you need to modify the <sessionState> tag to point to the custom table storage provider. Application name is the name recorded in the table when the session detail is logged, used to help identify the session.
<system.web> <sessionState mode="Custom" customProvider="TableStorageSessionStateProvider"> <providers> <clear/> <add name="TableStorageSessionStateProvider" type="Microsoft.Samples.ServiceHosting.AspProviders.TableStorageSessionStateProvider" applicationName="myWebAppName"/> </providers> </sessionState>
The sample code for the AspProviders project is availbe from Microsoft. Search for “Windows Azure Platform Training Kit”. Presently June 2010 is the latest release. Looking at the TableStorageSessionStateProvider.cs namespace you can see the code still resides in the Microsoft.samples namespace, hopefully this code will be rolled up into the main Windows Azure SDK at some point in the future.
You can either include the AspProviders project into your solution or compile the DLL and include that. Whatever you choose make sure you add it as a reference to your web role project.
The most important setting here is the “DataConnectionString”. The AspProviders uses this to pull all the rest of the settings needed to access the table store for your account, the rest of the settings here are standard for Azure. I’ve shown the values needed for running in the cloud environment when deployed.
<Role name="myWebAppName"> <Instances count="4" /> <ConfigurationSettings> <Setting name="AccountName" value="yourStorageAccountName" /> <Setting name="AccountSharedKey" value="yourStorageKey" /> <Setting name="BlobStorageEndpoint" value="https://blob.core.windows.net/ " /> <Setting name="QueueStorageEndpoint" value="https://queue.core.windows.net/ " /> <Setting name="TableStorageEndpoint" value="https://table.core.windows.net/ " /> <Setting name="allowInsecureRemoteEndpoints" value="false" /> <Setting name="DataConnectionString" value="DefaultEndpointsProtocol=https; AccountName=yourStorageAccountName; AccountKey=yourStorageSharedKey " /> </ConfigurationSettings> </Role>
At this point you will need to add the setting name keys into your ServiceDefinition.csdef or your will get a build error.
You will need to set the configuration setting for the Azure CloudStorageAccount before you try and access the table storage. This is best done in the Global.asax of the web role project
CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) => configSetter(RoleEnvironment.GetConfigurationSettingValue(configName)));
If all has gone to plan you can run your application and connect to the live Windows Azure services. Using a tool like Cerebrata Cloud Storage Studio you can look in your table store and see that a new table will be automatically created for you called “session”. If you perform a task requiring state in your application you should see serialised version of your session data stored in the table.