This post in continuation of previous post Implementing Session State Management in Azure Roles - Part1. Please verify for the introduction and the problems in normal method.
How to Solve?
To solve the issue, we can use Azure Table Storage service to store the session details by serializing it and gets back by de-serializing it. So the sessions must able to serialize. Alternately we can use App-fabric Caching, also to store the session details. So let’s try to modify the same code.
Step 1: Add AspProvider assembly in the WebRole project. This AspProvider is access the storage account and stores the session details in Table service.
The AspProvider project source code can find in the Azure tool kit under any of the following path.
You can compile the project, take the output dll file and add the reference to WebRole project. Alternatively, the source code under this post also has the same dll (taking from toolkit will be updated version).
<sessionState mode="Custom" customProvider="TableStorageSessionStateProvider"> <providers> <clear/> <add name="TableStorageSessionStateProvider" type= "Microsoft.Samples.ServiceHosting.AspProviders.TableStorageSessionStateProvider" applicationName="WebRole"/> </providers> </sessionState>Here, we as informing the runtime to use TableStorageSessionStateProvider of AspProviders to store the session values into Table service.
Step 3: Open the WebRole properties by expanding Azure project and selecting the properties of WebRole under Roles node.
Step 4: Select the Settings tab and add a setting as shows below.
Step 5: Save all files and run.
Once the Webcome.aspx page show, provide the User Name and Role and Press Login. The system will redirect to Default.aspx page with session details provided in Welcome.aspx page.
Click the Home and About menus, you will notice even the instance change the session details shows the same values.
Where the data gets stored?
As we know, the data gets stored in Azure Table Service. But, as we did not create any table for that, where the session data gets stored?
To store the session details, AspProvider creates a default table called Sessions and store each session values as separate each row. But, as the values as serialized it won’t be the exact values which was stored in the session. Whenever a new session values are created, there will a new row created in the Session table.
By using Azure Storage Explorer or Windows Azure MMC, we can verify the Sessions table. Below screen shot shows the same from my local.
Publishing to Azure
As pointed out earlier, when the project publishing to Azure environment. Two main points needs to remember.
- The AspProvider.dll must also be a part of package. So confirm the Copy Local property to True.
- The Storage Account connection string needs to be change to Azure storage account in ServiceConfiguration.Cloud.cscfg file. For that, get the Storage Account Key from the management portal and follow the below step.
- From the Settings tab of Web Role property window, click the ... button at DataConnectionString setting. It will popup Storage Account Connection String window.
- Select Enter storage account credentials and give Account Name, Account Key and select Use default HTTPS endpoints. click OK
- This will modify the DataConnectionString in ServiceConfiguration.Cloud.cscfg file.
- Publish the application and host it on Azure and verify.
Download the source code in C# here.