title: ASP.NET randomly losing session values
body: <p>I've been searching for answers for quite some time on this as it continues to plague me.  We store user login info and other data about the user's current activities in Session State (InProc).  Every so often I get a Null Reference exception trying to use one of the session variables.  It happens on random pages, with random Session variables.  I have modified the web.config httpRuntime and compliation tags to prevent appPool restarts:</p>
  
  <pre><code>&lt;httpRuntime requestValidationMode="2.0" waitChangeNotification="86400" maxWaitChangeNotification="86400" /&gt;
  &lt;compilation debug="False" strict="false" explicit="true" targetFramework="4.0" numRecompilesBeforeAppRestart="1000" /&gt;
  </code></pre>
  
  <p>I have set IIS to restart the app pool at 3am to make sure it doesnt restart when people are busy using the server.  And I'm logging app pool restarts in the event log to make sure I know when its happening.  </p>
  
  <pre><code>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 &amp; vbCrLf), EventLogEntryType.Warning)
  </code></pre>
  
  <p>I get the event log entries when the app pool restarts. 
  The App Pool is NOT restarting when these errors happen.</p>
  
  <p>When particular Session variables are lost, most of the other Session variables for the same user are still in place.  Also, there are typically another 10-20 users logged into the site that are unaffected when it happens.<br>
  The user that gets the error will back up, go through the same pages again, and it will work fine.</p>
  
  <p>I was having this problem on a Windows Server 2003 (32bit) running IIS6 with .NET 3.5 32bit and 4GB of memory.. As part of our server upgrades about a year ago we got a new webserver - Windows Server 2008 (64bit) running IIS 7 with 16GB memory.  I upgraded the website to .NET 4.0 64bit.  Still having the same problems on the new machine (usually 1-3 times per day - at random times through the day).</p>
  
  <p>I cant make it happen in my debugging due to its random nature, but I do believe it happens randomly on our dev environment as well.  The dev server has virtually the same specs as the production one.<br>
  Both environments are isolated and running as a single web server, not a part of a web farm.</p>
  
  <p>I'm thinking that I may try to implement a State Server to get out of the InProc mode, but that's just another stab in the dark..<br>
  Other than trying the State Server, is there anything else I can do to identify when this happens or prevent it?</p>