title: Sessions and server variables in Session_End
body: <p>I have an InProc session website with the following implementation:</p>
  
  <pre><code>protected void Session_End(object sender, EventArgs e)
          {
              StreamWriter sw = null;
              string logFile = Server.MapPath("testSessionLog.txt");
              sw = new StreamWriter(logFile, true);
              sw.WriteLine("Session_End called at " + DateTime.Now);
              try
              {
                  //Request is not available in this context 
                  if (Request.ServerVariables["Logon_User"] != null &amp;&amp; Request.ServerVariables["Logon_User"] != "")
                  {
                      sw.WriteLine("Made it past Request.ServerVariables check");
                  }
              }
              catch (Exception ex)
              {
                  sw.Write("An error occured: " + ex.Message);
              }
              finally
              {
                  sw.Close();
              }         
          }
  </code></pre>
  
  <p>I read a couple articles (<a href="https://stackoverflow.com/questions/464456/httpcontext-current-session-vs-global-asax-this-session">1</a>,<a href="https://stackoverflow.com/questions/2030113/asp-net-session-variables-on-session-end">2</a>) on stack that go into details about using this.Session to get to get to HttpSessionState.  What about Server.MapPath() and Request.ServerVariables()?  I can't get these to work within Session_End either.  </p>
  
  <p>I pasted this same blob of code into a button_Click event and it runs without issues.  This leads me to believe that it is something related to Session_End.  </p>
  
  <p>I setup IIS6 to recylcle once per minute.  When I have an open session it blows up with:
  <strong>Error:</strong><br />
      Exception type: HttpException <br />
      Exception message: Server operation is not available in this context</p>
  
  <p>In the event viewer it shows up as event 1309.  It complains about the line containing Server.MapPath()</p>