【问题标题】:Why would Session_Start keep reloading without firing Session_End为什么 Session_Start 会在不触发 Session_End 的情况下继续重新加载
【发布时间】:2016-05-12 18:20:00
【问题描述】:

我无法追踪为什么会话在网络应用程序中不断重启。

我已经在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 从未触发并且我无法记录它结束的原因。

我继承了这个“项目”,并在这一点上“探索”了代码,所以感谢你在这方面给我的任何帮助......

服务器是 IIS7,运行 ASP.NET,代码在 VB.NET 中,我还在 web.config 文件中包含了 '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 过程多次触发,我可能能够追踪会话重新启动的原因。

【问题讨论】:

  • 哪种语言?哪个服务器?一些代码?
  • IIS7,ASP.NET VB,(我确实在 web.config 中有 )我在会话启动过程中设置了许多会话变量,如下所示:
  • 我这样设置了一个会话变量: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 这是结束会话代码:Sub Session_End(ByVal sender As Object, ByVal e As EventArgs) Dim runtime As HttpRuntime = ... End SUB 希望能给你足够的继续
  • EndSession & End_Application 中的代码获取会话信息并将其写入日志
  • 对不起,这是一团糟,编辑问题,不要只是在 cmets 中添加代码。

标签: asp.net session-state


【解决方案1】:

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

请注意,如果您在 web.config sessionState 指令中使用 SQLServer 会话状态存储,或者实际上不是 InProc 模式,Session_End 将永远不会运行。

【讨论】:

  • 感谢詹姆斯的文章参考!在阅读了它和其中的几个链接之后,我发现了问题所在。添加了新代码以确定浏览器是否接受 cookie,然后在禁用 cookie 的情况下进行测试。启用 cookie 时不会发生这种情况!
【解决方案2】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-03
    • 1970-01-01
    • 2019-04-01
    • 1970-01-01
    • 2017-05-14
    • 2011-10-02
    • 1970-01-01
    • 2016-08-06
    相关资源
    最近更新 更多