0

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.

David DIlley
  • 31
  • 10
  • which language? which server? some code? – Christian Cerri May 10 '16 at 04:12
  • IIS7, ASP.NET VB, (and I do have in the web.config) I set a number of session variables in the session start procedure like this: – David DIlley May 10 '16 at 04:22
  • I set a session variables in the like this: Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs) HttpContext.Current.Session.Item("SessionMessageView") = "no" HttpContext.Current.Session.Item("DefaultMenuName") = "Default" HttpContext.Current.Session.Item("RootVirtualPath") = "/" ... End Sub And this is the end session code: Sub Session_End(ByVal sender As Object, ByVal e As EventArgs) Dim runtime As HttpRuntime = ... End SUb Hope that give you enough to go on – David DIlley May 10 '16 at 04:28
  • The code in the EndSession & End_Application gets info on the session and writes it to the log – David DIlley May 10 '16 at 04:30
  • sorry this is a mess, edit the question, don't just add code in comments. – Christian Cerri May 10 '16 at 04:33
  • Can't edit says can only edit for 5 mins and was posted 7 minutes ago ... – David DIlley May 10 '16 at 04:36
  • Basically, there is code in the Session_Start that sets session variables and code in the Session_End that try to log why the session ended – David DIlley May 10 '16 at 04:38
  • There are over 100 aspx pages with VB code behind them all, 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 break there so I can tell when it reloads and loses all the session variables set in ASPX page code.behind. – David DIlley May 10 '16 at 04:44
  • Sorry, misunderstanding your edit comment ... I am rather new to this forum ... I am 67 and "semi-retired". I worked for ATT in IT for a number of years and now I just "help" a few friends out part time. I edited the question to include more details, but did not post any code except for noting that I set session variables in the start procedure. There are so many pages of code in the site I would not know where to start with anything that might be relevant to the issue. If I could get the Session End procedure to fire that might help a lot, – David DIlley May 11 '16 at 09:52

2 Answers2

1

See this question regarding Session_Start : Session_Start firing multiple times on default ASP.NET MVC3 project

Note that Session_End will never run if you are using SQLServer session state storage, or indeed anything other than InProc mode in your web.config sessionState directive.

Community
  • 1
  • 1
James McCormack
  • 9,217
  • 3
  • 47
  • 57
  • Thanks for the article reference James! After read through it and several links from it, I have discovered the problem. New code was added to determine if the browser accepts cookies and then was being tested with cookies disabled. When cookies are enabled this does not happen! – David DIlley May 12 '16 at 10:08
0

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

David DIlley
  • 31
  • 10