Saturday, 20 August 2011

Implementing Session State Management in Azure Roles - Part2


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.

C:\WAPTK\Labs\WindowsAzureDeploymentVS2010\Source\Assets\AspProviders
C:\WAPTK\Labs\BuildAspNetAppsWithWindowsAzure\Source\Assets\AspProviders

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).
Note: After adding the reference to Web Role project, set the Copy Local property True as this dll will not be available in Azure environment once deployed.
Step 2: Open the Web.Config file and search if any sessionState node already exist. This node may exist as we already executed the project by storing some value in the session object.
Replace the sessionState node with the following.
<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.
Note: This setting specifies the storage account which required storing the session details. Here, we use UseDevelopmentStorage=true to point to storage emulator as we are testing with local system. When the same project moved to cloud, the storage account connection string needs to be replaced.

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.
  1. The AspProvider.dll must also be a part of package. So confirm the Copy Local property to True.
  2. 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.
    1. From the Settings tab of Web Role property window, click the ... button at DataConnectionString setting. It will popup Storage Account Connection String window.
    2. Select Enter storage account credentials and give Account Name, Account Key and select Use default HTTPS endpoints. click OK
    3. This will modify the DataConnectionString in ServiceConfiguration.Cloud.cscfg file.
    4. Publish the application and host it on Azure and verify.

Download the source code in C# here.

2 Responses to “Implementing Session State Management in Azure Roles - Part2”

  • Anonymous says:
    18 September 2012 at 11:37

    When 2 instances were changed then why session variables were not saved..
    I understand Storage table will help. What happened after changing from one instance to 2 instances..Why did the webrole couldn't capture the session variable from welcome to default. pls explain..

  • Thirumalai M says:
    18 September 2012 at 22:36

    Hi, Can you pls read the previous post of the same topic from the below url -
    http://www.dotnettwitter.com/2011/08/handling-session-in-azure-webrole-part1.html

    I could not fully understand you doubt, pls contact me by mail.

Post a Comment