Incorrect ASP.NET session data returned intermittently

asp.netiis-7.5windows-server-2008-r2

I've been given the task of looking into a problem where the session of a user changes between refreshes of a page. Immediately (not knowing anything about the website) I said that it seems like a load-balancing/web cluster configuration issue. However, I found out later that the website is not load-balanced – It's a single server.

The application has a Basket object which is stored in Session. The user adds items to the basket etc. When on the basket page, if the page is refreshed the items which are in the basket change and sometimes there are none.

The session is not lost, it comes back almost always on the next refresh but then the basket can be empty or have different content on the next refresh. I've seen this behaviour before many times but only on multi-server set-ups.

This is something we have tried to replicate on a developer environment with no success – we simply cannot replicate the problem but on live it happens a lot.

The web application is an ASP.NET WebForms site, running under .NET 4.0 on Windows Server 2008 R2 and IIS 7.5.

Session state mode is InProc and the session timeout is set to 480 minutes.

I've used fiddler to look at the requests and responses and the ASP.NET session ID passed to the server is the same each time. There appears to be no difference between the responses besides the actual HTML returned.

Has anyone got any ideas on what could cause this problem?

Update 1:

I have re-worked some of the logging that a colleague implemented in the basket code. I can now see that the Session ID remains the same, the value of the Session variable also stays the same even though different data is displayed.

Sorry, I should have checked their logging before posting this.

I'm convinced that this is a server issue though, the code works as expected in our testing environment.

Best Answer

"I said that it seems like a load-balancing/web cluster configuration issue. However, I found out later that the website is not load-balanced - It's a single server."

Just because it's a single server doesn't mean there's not load balancing going on. IIS has within it a feature called a "Web Garden" which splits the web work across more than one process, so that it can be spread across multiple CPUs.

It's like a small "Web Farm" -- hence the name "Web Garden". :)

An effect when using a Web Garden is that it makes InProc session variables stop working correctly, as you may end up in a different worker process on each page request. InProc (In Process) only works reliably if you always end up in the same Process.

Solution 1:

  • Ensure IIS isn't set to use a Web Garden. To do this:
    1. Open IIS manager.
    2. Determine which App Pool you web app is using.
    3. Right-click your App Pool and select "Advanced Properties".
    4. Scroll down and set "Maximum Worker Processes" to 1.

App Pool Settings

Solution 2:

Check out this MSDN blog: In-Proc Session State Management

It describes your session state options, and how to troubleshoot InProc session loss.