In-Proc Session Does Not Work Well in Windows Azure

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.

image_0EAFE89B

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.

image_5C704A85

3. In the default.aspx page, I simply insert several ASP.NET component as shown below.

   1: <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
   2:     <h2>
   3:         Welcome to ASP.NET!
   4:     </h2>
   5:     <p>
   6:         Enter value for session "CustomerName" : 
   7:         <asp:TextBox runat="server" ID="txtCustomerName" />
   8:         &nbsp;<asp:Button Text="Save Session" ID="btnSaveSession" runat="server" 
   9:             onclick="btnSaveSession_Click" />
  10:         <br />
  11:         <br />
  12:         <asp:Button Text="Display Session" ID="btnDisplaySession" runat="server" 
  13:             onclick="btnDisplaySession_Click" />
  14:         <br />
  15:         <asp:Label Text="" ID="lblSessionValue" runat="server" />
  16:     </p>
  17: </asp:Content>

4. Next, go to default.aspx.cs and type the following code:

   1: public partial class _Default : System.Web.UI.Page
   2: {
   3:     protected void Page_Load(object sender, EventArgs e)
   4:     {
   5:  
   6:     }
   7:  
   8:     private const string SESSION_NAME_KEY = "CustomerName";
   9:  
  10:     protected void btnSaveSession_Click(object sender, EventArgs e)
  11:     {
  12:         Session[SESSION_NAME_KEY] = txtCustomerName.Text;
  13:         System.Diagnostics.Trace.TraceInformation("New value of session CustomerName : {0} is stored in {1}.", 
  14:             txtCustomerName.Text, RoleEnvironment.CurrentRoleInstance.Id);
  15:     }
  16:  
  17:     protected void btnDisplaySession_Click(object sender, EventArgs e)
  18:     {
  19:         System.Diagnostics.Trace.TraceInformation("Request is handled by " + RoleEnvironment.CurrentRoleInstance.Id);
  20:         if (Session[SESSION_NAME_KEY] != null)
  21:             lblSessionValue.Text = "Value for CustomerName is : " + Session[SESSION_NAME_KEY].ToString();
  22:     }
  23: }

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.

image_418F2906

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.

image_25219B4C

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:”

image_39FFA59A

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.

image_19081EF4

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.

image_2B5821DE

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.

image_25A91A06

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.

See you.

This entry was posted in Uncategorized. Bookmark the permalink.

2 Responses to In-Proc Session Does Not Work Well in Windows Azure

  1. Pingback: Implementing Session State Management in Windows Azure with Azure Storage | Wely's Cloud Journey...

  2. Pingback: Various Options to Manage Session State in Windows Azure | Wely's Cloud Journey...

Leave a Reply

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

*