2

I have an InProc session website with the following implementation:

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 && 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();
            }         
        }

I read a couple articles (1,2) 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.

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.

I setup IIS6 to recylcle once per minute. When I have an open session it blows up with: Error:
Exception type: HttpException
Exception message: Server operation is not available in this context

In the event viewer it shows up as event 1309. It complains about the line containing Server.MapPath()

Community
  • 1
  • 1
P.Brian.Mackey
  • 43,228
  • 68
  • 238
  • 348

3 Answers3

4

As already mentioned there is no HttpContext object to work with when Session_End is being called, so accessing ie. ServerVariables doesn't make any sense at all.

For MapPath you can call the static method HostingEnvironment.MapPath() which doesn't rely on being in a request.

http://msdn.microsoft.com/en-us/library/system.web.hosting.hostingenvironment.mappath.aspx

Pauli Østerø
  • 6,878
  • 2
  • 31
  • 48
  • We use ServerVariables to grab the user's UserName. Being that all users are on the domain I don't understand how getting Logon_User doesn't make sense when there's still a this.Session. Unless you are telling me that this.Session within Session_End is a session fired off by the System and containing only system information. Is that the case? – P.Brian.Mackey Jan 13 '11 at 14:41
  • Yes. Session_End is fired at some arbitrary point that you can't control. It can be at the middle of the night when your user is long gone and sleeping tight. The important thing to understand is that there *is no HttpContext* to access because the event is not fired during some request. – Pauli Østerø Jan 13 '11 at 17:24
2

The problem is that session_end is not fired from a request. it is triggered by a time out on the server, long after the last request on the sesion has been processed. So, there is no request object to be had.

This may not be true if you have called Abandon on the session (since you have done that within the context of a request). I haven't tried this, but I suspect it would not work either.

I don't know about MapPath - maybe it requires a live Request to do its thing.

Ray
  • 21,485
  • 5
  • 48
  • 64
  • You would think that the system knows which session to end and therefore can relate some of the original session data back to this.Session and store it in this.Session.original before blowing it away... – P.Brian.Mackey Jan 13 '11 at 14:54
  • You were looking for Request data, and there is no Request object in Session_End. If there is stuff you need to access there (say, as user id or something), then store it explicitly in the session: *Session["UserId"] = _userId* – Ray Jan 13 '11 at 15:52
  • I'm actually not sure what we're looking for. There's 12 apps and I didn't write any of them. I'm just troubleshooting why we get 100's of errors a day. Since the "solution" I posted below gets rid of the errors, that's the "fix". – P.Brian.Mackey Jan 13 '11 at 17:25
  • Well, good luck - I hate working on other people's lousy code (I would rather work on my own...) – Ray Jan 13 '11 at 17:27
-2

It is possible to get to Request and Server after all. You have to use HttpContext.Current.Request and HttpContext.Current.Server respectively. Not session. There's a pretty good comparison of the two on stack.

Community
  • 1
  • 1
P.Brian.Mackey
  • 43,228
  • 68
  • 238
  • 348