Excluding InProc Session Data
I just noticed this about a week ago. I store data about the current puzzle the user is playing (www.wikipediamaze.com):
HttpContext.Current.Session.Add ("puzzleInfo", currentPuzzleInfo);
I know that storing data in a session using "InProc" mode is very volatile and will get reset whenever the web.config or any number of other factors changes, including application pool reuse.
However, my data only stays for a few seconds (time is variable, but literally not for long) and then disappears. I'm in a general hosting environment, so I don't know if this would have anything to do with it.
Any idea what's going on? Would I be better off storing it directly on the client as a cookie? Please, help.
source to share
Loss of data every few seconds is unusual, there may be a problem with your hosting provider - it seems that the server is constantly dropping sessions for some reason. You can display sessionID (Session.SessionID) and see if it changes.
The process might crash caused by someone else on the server. Here is a msdn blog post about fixing this issue (scroll around 25% of the path): http://blogs.msdn.com/webtopics/archive/2009/07/22/in-proc-session-state-management.aspx
It won't help you in a co-location environment, but you can pass it to the support chain. Perhaps they can look at the event logs and determine who is breaking IIS and download them.
As Sean McDonough stated:
... considering that you are using a shared hosting environment, there is a decent chance that there is more than one web server hosting your site. If multiple servers are serving your site and the load balancing mechanism is in use and does not support any session affinity, you can simply bounce to another web server and start a new session; after all, an in-proc session will not follow you between fields.
Alternatives to state resilience
Anyway, I've always found InProc to be fatally unreliable even on dedicated servers, so I avoid it.
And by that I mean that you are going out of best practice and might run into some ugly situations, like at that time I needed to save an XML document for a second. That wouldn't be a good idea in a cookie!
If you don't have a way to set up a Session State Server (which is very easy by the way) because you are in a co-located environment, you can look into SqlSessionStateStore, which allows you to store your session data in SQL Server.
source to share
Brian's suggestions are excellent and I couldn't agree more with what he was doing. I would also like to suggest another possibility.
Do you know a lot about the environment your application is hosted in? The reason I ask is, given that you are in a co-hosted environment, there is a decent chance that your site is hosting more than one web server. If multiple servers are serving your site and you are using a load balancing mechanism that does not support any similarity in the session, you can simply hop to another web server and start a new session; after all, the in-proc session won't follow you through the fields.
Food for thought: -)
source to share