Managing ASP.NET Session State with Windows Azure AppFabric Cache
As has been published on numerous blogs now, Windows Azure AppFabric Cache is now in production and free to use until August 1, 2011. See here for the official announcement: http://blogs.msdn.com/b/windowsazure/archive/2011/05/02/just-released-windows-azure-appfabric-caching-service.aspx
Now, there is a wonderful sample and video on codeplex (http://appfabricdemos.codeplex.com/releases/view/65427) that simulates how the Session State Management works. However, I don’t like simulations so I took my existing code from this blog post http://blogs.msdn.com/b/danbuche/archive/2011/03/29/migrating-asp-net-session-state-to-the-sample-tablestoragesessionstateprovider.aspx, complete with the “Process ID” indicator that will help me understand which instance is now really serving up my data, and implemented AppFabric Cache using the code snippets provided in the Windows Azure portal.
And it really is that simple – again, all that you need to copy into your web.config is precreated for you in the Windows Azure Management portal, you won’t need to type a single line or even touch any of your code outside of web.config:
BIG Caveat – don’t try this at home!
Now what do I mean with that? What surprised me at first was that the session state caching didn’t seem to be working in my local Compute Emulator. When running this against multiple instances, I got separate session states when hitting other instances. No problem whatsoever running it live in the cloud.
Why is that?
I had already hit this with the SQL Session State provider, so this was quite easy to understand – the Session State providers tend to use the IIS Site Name to identify individual web applications to avoid sharing session state between different apps (which is a very sane thing to do, from a security perspective). And this is where you have a distinct difference between your local compute emulator and a live deployment.
In the production environment, all of your web roles will host your application under the same Site Name on the different instances. In your emulator, on the other hand, all needs to be handled by one IIS Server, so you end up with different Site Names for each instance.
So, in essence, you won’t be able to test session state management with multiple web role instances on your compute emulator.