I have been looking for answers to this for quite some time, as it continues to hurt me. We store user login data and other data about current user activities in session state (InProc). Each time I get a Null Reference exception, trying to use one of the session variables. This happens on random pages with random session variables. I changed the web.config httpRuntime and matching tags to prevent appPool from restarting:
<httpRuntime requestValidationMode="2.0" waitChangeNotification="86400" maxWaitChangeNotification="86400" /> <compilation debug="False" strict="false" explicit="true" targetFramework="4.0" numRecompilesBeforeAppRestart="1000" />
I installed IIS to restart the application pool at 3am to make sure it does not restart when people are busy on the server. And I am registering the application pool in the event log to make sure that I know when this happens.
Dim runtime As HttpRuntime = GetType(System.Web.HttpRuntime).InvokeMember("_theRuntime", BindingFlags.NonPublic Or BindingFlags.Static Or BindingFlags.GetField, Nothing, Nothing, Nothing) Dim shutDownMessage As String = runtime.GetType().InvokeMember("_shutDownMessage", BindingFlags.NonPublic Or BindingFlags.Instance Or BindingFlags.GetField, Nothing, runtime, Nothing) Dim shutDownStack As String = runtime.GetType().InvokeMember("_shutDownStack", BindingFlags.NonPublic Or BindingFlags.Instance Or BindingFlags.GetField, Nothing, runtime, Nothing) Dim evtSource As String = "ASP.NET" Dim log As New EventLog log.Source = evtSource log.WriteEntry(String.Format("_shutDownMessage={0}{2}_shutDownStack={1}", shutDownMessage, shutDownStack, vbCrLf & vbCrLf), EventLogEntryType.Warning)
I get event log entries when the application pool restarts. The application pool does not restart when these errors occur.
When certain session variables are lost, most other session variables for the same user still exist. In addition, as a rule, another 10-20 users register on the site, which are not affected when this happens.
The user who receives the error message will back up, repeat the same pages, and it will work normally.
I had this problem on Windows Server 2003 (32-bit) with IIS6 with .NET 3.5 32-bit and 4 GB of memory. As part of our server updates, about a year ago, we received a new web server - Windows Server 2008 (64-bit) with IIS 7 with 16 GB of memory. I upgraded the site to .NET 4.0 64bit. The same problems still occur on the new machine (usually 1-3 times a day - randomly during the day).
I cannot do this in my debugging due to its random nature, but I am sure that this happens randomly in our development environment. The dev server has almost the same characteristics as the production server. Both environments are isolated and run as a single web server, not part of a web farm.
I think I can try to implement State Server to exit InProc mode, but this is just another hit in the dark.
Besides trying the status server, is there anything else I can do to determine when this will happen or to prevent it?