为什么 Session_Start 会在不开火的情况下继续重新加载 Session_End

Why would Session_Start keep reloading without firing Session_End

我无法找到会话在 Web 应用程序中不断重启的原因。

我已经在 Session_EndApplication_End 过程中放置​​了代码来尝试跟踪正在发生的事情,但是在会话重新启动并触发 Session_Start 再次程序。

有谁知道什么会导致 Session_Start 程序在不触发 Session_EndApplication_End 程序的情况下重新启动?

基本上,Session_Start 中有设置会话变量的代码,Session_End 中有代码试图记录会话结束的原因

网站上有 100 个或更多带有 VB 代码的 aspx 页面,看起来会话随机重新启动,有时在加载主页时多达 3 次。我在 Session_Start 过程中的 global.asax 页面中设置了一个中断,它在那里中断,所以我可以知道它何时重新加载并丢失在后面的任何 ASPX 页面代码中设置的所有会话变量。

我知道一些常见的原因,比如写入某些文件或文件夹,如 App_Data 文件夹,以及应用程序池被回收,但我似乎无法找到为什么 Session_EndApplication_End 从不触发,我无法记录它结束的原因。

我继承了这个 "project" 并且我 "winging" 在这一点上完成了代码所以感谢你能给我的任何帮助...

服务器是 IIS7,运行 ASP.NET 代码在 VB.NET,我还在 [=58] 中包含了 'sessionState mode="InProc" ' =] 文件以确保将使用会话过程。

这是 Session_Start 过程中的 VB 代码

    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

这是 Session_EndApplication_End 过程中的 VB 代码

    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])

如果我只能弄清楚为什么 Session_End 过程没有触发而 Session_Start 过程多次触发,我也许能够找到会话重新启动的原因。

请参阅有关 Session_Start 的问题:Session_Start firing multiple times on default ASP.NET MVC3 project

请注意,如果您正在使用 SQLServer 会话状态存储,或者 web.config 中除 InProc 模式以外的任何其他模式,Session_End 将永远不会 运行 sessionState 指令。

在 James 的帮助下,我发现了这个问题。添加了新代码以确定用户的浏览器是否接受 cookie,然后在禁用 cookie 的情况下对其进行测试。当启用 cookie 时,这不会发生并且会话不会重新启动。似乎当浏览器不接受 cookie 并且您尝试在该浏览器上存储 cookie 时,由于没有触发的错误导致会话重新启动 End_Session!

现在我想我们将不得不重新考虑 cookie 测试并找到一种方法来存储以某种方式链接到用户的标志,并且可以指示 cookie 是否被接受而无需尝试在用户的机器上存储 cookie .. .嗯嗯

感谢大家在这方面的帮助...有时是小事让您失望...我想起了这样一句话:错误的增加与代码的变化成正比