Implementing Session State Management in Windows Azure with Azure Storage

In last post, I’ve shown you why and how the InProc session doesn’t work in Windows Azure. In this post, I’ll show you how to work around this issue, specifically using Azure Storage as Custom Session Provider. Well, actually it’s also possible for you to use other provider such as SQL.
AspProviders actually not only mean to be used for Session, it also available for Profile and Membership implementation in Azure Storage. However, I’ll only focus on the session state in this post. Session is implemented by using Table and Blob Storage to store the hashed value of our session. Don’t worry about the complexity because the way we access session in our application is exactly the same. You still use Session[“name”] to set and get the value.

How to use

0. Regarding the sample project, please refer to my previous post the continue if have not do so.
1. Download the provider that was originally uploaded on Windows Azure Code Sample. To simplify and avoid any confusion, I also upload the provider here as compiled dll format  or the entire complete solution.
2. Reference the AspProviders dll / project to your main intended project. (In this example, I use dll file only).
3. You will need to add some configuration in web.config as following (below the configuration section):
   1: <configuration>


   2:   <appSettings>


   3:     <!-- account configuration -->


   4:     <add key="TableStorageEndpoint" value=""/>


   5:     <add key="BlobStorageEndpoint" value=""/>


   6:     <add key="AccountName" value="devstoreaccount1"/>


   7:     <add key="AccountSharedKey" value="Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw=="/>


   8:     <add key="DefaultSessionTableName" value="Sessions"/>


   9:   </appSettings>


  10:   ....

Do note that in the example above, I point the value of both TableStorageEndpoint as well as BlobStorageEndpoint to my local storage account. The same thing applies in AccountName and AccountSharedKey, I also point them to my local dev storage account. When uploading to Azure environment, do remember to change those configuration as required.
Alternately, you may also want to change the DefaultSessionTableName if you want to be stored in different table name.
4. Still in web.config file, locate to “system.web” section and insert the following configuration. This is to tell the ASP.NET engine that we will use “custom” session mode that was implemented with TableStorageSessionStateProvider.
   1: <system.web>


   2:   <sessionState mode="Custom" customProvider="TableStorageSessionStateProvider">


   3:     <providers>


   4:       <clear/>


   5:       <add name="TableStorageSessionStateProvider" type="Microsoft.Samples.ServiceHosting.AspProviders.TableStorageSessionStateProvider" />


   6:     </providers>


   7:   </sessionState>


   8:   ...

Alright, that’s all done. Now, you may want to test our new configured session state works.

Let’s test it

1. I purposely, change the instance of my webrole to 3.
2. Just run the application as usual and enter “Bob” to the textbox as what we’ve done in the previous post.
You can see that, other than our log that was written with TraceInformation, the AspProvider actually stored something on the table and blob with respect to our session key and value.
3. Now click on the “DisplaySession” button to see whether you can get the value. I believe it can right Smile. Yes, since the value is not stored on the webrole’s memory, however on the table storage.
4. As I mentioned before, AspProviders will store our session key / value in form of “hashed” in the table and blob storage, depending the configuration we’ve done on which account to store. To enable you to clearly see those table and blob, you may want to use AzureStorageExplorer to examine further when the session is created.
Now, you can see that there’s a record exists on my “Sessions” table inside my local account.
You can also download my complete sample project here.
I hope this post is useful for you.
This entry was posted in Uncategorized. Bookmark the permalink.

1 Response to Implementing Session State Management in Windows Azure with Azure Storage

  1. Pingback: Exam Ref 70-486: Developing ASP.NET MVC 4 Web Applications | Jinal Desai .NET

Leave a Reply

Your email address will not be published. Required fields are marked *