ASP.NET Session_End event not firing

故事扮演 提交于 2019-12-19 07:52:17

问题


I'm trying to get a database transaction to execute when an ASP.NET session ends (in simple WebForms app on Windows 2008 Server). I realize that many respondents will suggest not to rely on the Session_End event (as I've read in a hundred forum posts on this topic yielded by a Google search). Please humor me.

I've done these things:

  1. Enabled session state in web.config: <sessionState mode="InProc" cookieless="false" timeout="1" />

  2. Confirmed that the Session_End event does NOT fire when I call Session.Abandon() OR when the session times out.

  3. Confirmed that the session IS timing out after 1 minute (as configured above).

  4. Confirmed that I'm not attempting to access any Request or Response or Server objects (which I understand would silently error out).

EDIT: I've also confirmed that I'm storing data in the session--I use a number of session variables to store different data points, and these are instantiated when the user logs in to the app.

EDIT: I've also confirmed that I'm not creating the session and then abandoning it in the same request. As noted above, the problem exists even when the session times out (i.e., no Request is made).

Please help!


回答1:


For others looking for a resolution and not finding an answer to this issue, I have seen a case where a project's Global.asax was defined with a code behind file referenced, but the events defined within the asax instead of the .cs file.

In this case, it would fire the Session_Start, but never the Session_End, as if it was looking in the referenced .cs file.

By removing the reference to the code behind file, Session_End started firing as expected.




回答2:


If you don't save anything into the session, the session_end will not fire. If you're saving data in the session in the first request, and calling abandon in the same request the session_end will also not fired.

Hope this helps!

T




回答3:


Turns out the event WAS firing, but the code that was being executed was silently erroring out, since I wasn't properly accessing the Application object and some of my App_Code classes (which I still don't know how to do, but is worthy of a separate Stack Overflow question).




回答4:


Try initializing the session from within the same method that queries the database.Do it before querying the database.



来源:https://stackoverflow.com/questions/15777004/asp-net-session-end-event-not-firing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!