How to Access ASP.NET Session State from Application_Error

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

For one reason or another I am unable to access Session contents in my
asp.net application via the global.asax's Application_Error event. I can
pull this exact code snippet below out of global.asax and place it in a
normal page (or class file that doesn't inherit from system.web.ui.page) and
things work fine:

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
Dim currentContext As System.Web.HttpContext =
System.Web.HttpContext.Current
Dim sessionInfo As System.Web.SessionState.HttpSessionState =
currentContext.Session
Response.Write("try to get session count")
Response.Write("<br />")
Response.Write(sessionInfo Is Nothing) ' always returns True
Response.Write("<br />")
Response.Write(sessionInfo.Contents.Count) ' always throws a Null
Reference exception, so would sessionInfo("mySessionVar")
Response.End()
End Sub

Session state is currently being held In Process (default), and it doesn't
matter whether this application has any Session variables established or not
- I get the same result.

Can someone shed some light / provide some working code?
 
Daflookie,
When an unhandled exception occurs in ASP.NET the current session will be
ended and a new session is started - this makes the former session items
unavailable.
--Peter
 
Peter, thanks for the response. I'm hoping I'm hearing you wrong. :)

Are you saying that when an end user experiences an unhandled exception that
user's session state is lost? Similar to recycling the application pool,
only this time for just the user that experienced the error?

For example if an ecommerce module uses sessions to identify what cart ID
the user currently has and they experience an error during checkout the cart
ID held in session would be lost?

Thanks,
Steve
 
Back
Top