Search code examples
asp.netsession-state

Why would Session_Start keep reloading without firing Session_End


I am having trouble tracking down why the session keeps restarting in a web app.

I have placed code in both the Session_End or Application_End procedures to try and track what is happening, but neither of these two procedures are reached in code before the session restarts and fires the Session_Start procedure again.

Does any know what would cause the Session_Start procedure to restart without firing the Session_End or Application_End procedures?

Basically, there is code in the Session_Start that sets session variables and code in the Session_End that tries to log why the session ended

There are 100 or more aspx pages with VB code behind them all on the web site, and it looks like the session restarts randomly, sometime up to 3 times while loading the home page. I have a break set in the global.asax page inside the Session_Start procedure and it breaks there so I can tell when it reloads and loses all the session variables set in any of the ASPX page code behind.

I know some common causes like writing to certain files or folders like the App_Data folder, and the app pool being recycled, but I can not seem to track down why this is happening when Session_End or Application_End never fire and I cannot log the reason it ended.

I inherited this "project" and I "winging" my way through the code at this point so thanks you for any help you can give me on this ...

the server is IIS7, running ASP.NET and the code is in VB.NET, I have also included ' sessionState mode="InProc" ' in the web.config file to make sure the session procedures will be used.

This is VB code in the Session_Start procedure

    HttpContext.Current.Session.Item("SessionMessageView") = "no"
    HttpContext.Current.Session.Item("DefaultMenuName") = "Default"
    HttpContext.Current.Session.Item("RootVirtualPath") = "/"
    HttpContext.Current.Session.Item("BlockerTested") = False
    HttpContext.Current.Session.Item("BlockerTurnedOn") = False
    HttpContext.Current.Session.Item("IsMobileBrowser") = False

This is VB code in both the Session_End and Application_End procedures

    Dim runtime As HttpRuntime = DirectCast(GetType(System.Web.HttpRuntime).InvokeMember("_theRuntime", System.Reflection.BindingFlags.NonPublic Or System.Reflection.BindingFlags.[Static] Or System.Reflection.BindingFlags.GetField, Nothing, Nothing, Nothing), HttpRuntime)
    If runtime Is Nothing Then
        Return
    End If
    Dim shutDownMessage As String = DirectCast(runtime.[GetType]().InvokeMember("_shutDownMessage", System.Reflection.BindingFlags.NonPublic Or System.Reflection.BindingFlags.Instance Or System.Reflection.BindingFlags.GetField, Nothing, runtime, Nothing), String)
    Dim shutDownStack As String = DirectCast(runtime.[GetType]().InvokeMember("_shutDownStack", System.Reflection.BindingFlags.NonPublic Or System.Reflection.BindingFlags.Instance Or System.Reflection.BindingFlags.GetField, Nothing, runtime, Nothing), String)
    If Not System.Diagnostics.EventLog.SourceExists(".NET Runtime") Then
        System.Diagnostics.EventLog.CreateEventSource(".NET Runtime", "Application")
    End If
    Dim log As New System.Diagnostics.EventLog()
    log.Source = ".NET Runtime"
    log.WriteEntry([String].Format(vbCr & vbLf & vbCr & vbLf & "_shutDownMessage={0}" & vbCr & vbLf & vbCr & vbLf & "_shutDownStack={1}", shutDownMessage, shutDownStack), System.Diagnostics.EventLogEntryType.[Error])

If I could only figure out why the Session_End procedure is not firing while the Session_Start procedure fires multiple time I might be able to track down the why the session is restarting.


Solution

  • With help from James I have discovered the problem. New code was added to determine if the user's browser accepts cookies and then it was being tested with cookies disabled. When cookies are enabled this does not happen and the session does not restart. It seems that when cookies are not accepted by a browser, and you try to store a cookie on that browser, it caused the session to restart because of the error without firing End_Session!

    Now I guess we will have to rethink the cookie test and figure a way to store a flag that is somehow linked to the user and can indicate if cookies are accepted without trying to store a cookie on the user's machine ... hmmmm

    Thank you all for your assistance on this ... sometimes it's the small things that trip you up ... I am reminded of the saying that an increase in bugs is proportional to changes in code