A few things can lead to mysterious disappearances of the session state.
- Your sessionState timeout has expired
- Are you updating your web.config or another type of file that causes your AppDomain to recycle
- Your AppPool in IIS Recycle
- You are updating your site with a large number of files, and ASP.NET is actively destroying your AppDomain to recompile and save memory.
-
If you are using IIS 7 or 7.5, here are a few things to look for:
- By default, IIS forces AppPools to shut down after a period of inactivity.
- By default, IIS forces AppPools to recycle every 1740 minutes (obviously, depending on your root configuration, but by default)
- In IIS, check out the "Advanced Settings" of your AppPool. It has the "Idle Time-out" property. Set this value to zero or a greater number than the default value (20).
- In IIS, check the "Disposal" settings of your AppPool. Here you can enable or disable your AppPool from recycling. The second page of the wizard is a way to enter the event log of each type of AppPool.
If you use IIS 6, the same settings apply (for the most part, but with different ways to get them), but getting them to register the process is more painful. Here is a link to a way to force IIS 6 to log AppPool reuse events:
http://web.archive.org/web/20100803114054/http://surrealization.com/sample-code/getnotifiedwhenapppoolrecycles/
-
If you are updating files in your web application, you should expect the entire session to be lost. This is just the nature of the beast. However, you cannot expect this to happen several times. If you update 15 or more files (aspx, dll, etc.), there is a possibility that you will have several reboots within a certain period of time, as these pages will be recompiled by users accessing the site. See these two links:
http://support.microsoft.com/kb/319947
http://msdn.microsoft.com/en-us/library/system.web.configuration.compilationsection.numrecompilesbeforeapprestart.aspx
Setting numCompilesBeforeAppRestart to a larger number (or manually disabling your AppPool) will fix this problem.
-
You can always contact Application_SessionStart and Application_SessionEnd to receive notifications when a session is created or completed. The HttpSessionState class also has the IsNewSession property, which you can check on any page request to determine if a new session has been created for the active user.
-
Finally, if this is possible in your circumstances, I have successfully used SQL Server session mode . This is not recommended if you store a large amount of data in it (each query loads and stores the full amount of data from SQL Server), and it can be painful if you put custom objects in it (since they should be serializable), but it helped me in a shared hosting scenario where I could not configure my AppPool so as not to process a couple of hours. In my case, I saved limited information and did not have an adverse effect. Add to this the fact that an existing user will reuse their default SessionID, and my users have never noticed that their in-memory session was disconnected using the AppPool utility because all their state was saved in SQL Server.
Adam Sills Aug 18 '10 at 19:46 2010-08-18 19:46
source share