In traditional ASP.NET application, you have option to use several session states mode including In-Process, State Server, SQL Server, or even Custom. In fact, the simplest way and most common scenario is using In-Process where the session states is actually stored in the web server memory.
However, you will definitely face challenges when using in-proc session mode in Windows Azure. As the matter of fact, you may be fine if you are running only with one instance. The reason is because when you have more than one role instance, Windows Azure will create separate VMs for each role instance. As it, each role instance will own their own memory space that is not sync-up amongst other instance-roles. Well, it’s not a good idea to restrict our application to stick to one instance, since probably the reason you choose Windows Azure is the ability to scale in and out elastically.
Alright,let me simply show you why it’s challenging.
Running with one instance
1. The first step,I open Visual Studio and simply select New Project – Cloud – Windows Azure Cloud Service. And type in SessionDemo.
2. When the New Cloud Service Project dialog box comes out, select ASP.NET WebRole and change the name to SessionDemo_WebRole, and click OK.
3. In the default.aspx page, I simply insert several ASP.NET component as shown below.
4. Next, go to default.aspx.cs and type the following code:
What it does is just simply display the session value to the screen as well as write it to log by calling Trace.TraceInformation() method.
5. Run the application by click on Run button or F5. Fill-in “Bob” on the textbox and click on “SaveSession” button. When you click on “DisplaySession”, you will simply get “Bob” value which you’ve entered earlier.
6. Alternately, you can also notice the log at development fabric indicate that “New value of session CustomerName : Bob is stored in deployment(495).SessionDemo.SessionDemo_WebRole.0.”
Yes, so far so good. You may change to other value, I guarantee you that it’s still working well as per now.
Running with more than one instance
7. By default, Visual Studio will specify only 1 instance per role. If you need more, you can just simply modify it by click on the intended role and fill in the value of “Instance count:”
Now, change the instance count from 1 to 2.
8. Run your application by pressing F5 / click on Run button. At the mean time, open up your development fabric UI as well. You may want to clear your log to see it more clearly.
As usual, I type “Bob” on the browser and click on “SaveSession”. If you inspect on the dev fabric UI, you will notice that only one of the two role will be handling your request.
9. Click on the “DisplaySession” button to see whether you can see the value “Bob” is display. The answer is maybe yes, maybe no. If the instance 0 who are handling the request, then good that you would be able to retrieve the value. However, it’s also possible that instance 1 is the one who is handling your request. If this is the case, you will see no value displayed.
This proves that the in-proc session scenario doesn’t work in Windows Azure environment.
In the next post, I’ll show you how to use TableStorage Session to workaround this issue.