为什么在App Start上调用Session End? [英] Why Session End is called on App Start up ?

查看:90
本文介绍了为什么在App Start上调用Session End?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望释放一些数据库资源,并在ASP.NET站点中的用户会话结束时设置一些标志。但是当我编写我的代码以访问Global.asax文件的Session End方法中的会话变量时,每次应用程序启动时都会调用它,为什么会出现这种奇怪的行为?



其次,我想访问用户特定的会话变量,并在Session End上的DB中释放它们。因此,我使用的会话变量很少,我在页面上的web方法中设置它们并尝试在Session End上访问它们。但是,自从在App启动时调用Session结束时,它总是抛出Null引用异常。



这是mycode。在.aspx页面中设置Webmethod中的变量

I wanna free some DB resources and set few flags when User Session Ends in ASP.NET Site. But when I write my code to access session variables in Session End method of Global.asax file, its getting called everytime the App starts, Why this weird behavior?

Secondly, I want to access user specific session variables and free them up in DB on Session End. Thus I am using few session Variables where I am setting them in a webmethod on a Page and trying to access them on Session End. But since Session end is being called on App start up its always throws Null reference exception.

Here is mycode. for Setting up a variable in Webmethod in a .aspx page

[WebMethod(EnableSession=true)]
protected void checkUser()
        {
            Session["TestObject"] = "Hello I am session";
}





在我的global.asax文件中,我试图在Session_End方法中按如下方式访问





In my global.asax file I am trying to access as follows in Session_End method

void Session_End(object sender, EventArgs e)
        {
            // Code that runs when a session ends. 
            // Note: The Session_End event is raised only when the sessionstate mode
            // is set to InProc in the Web.config file. If session mode is set to StateServer 
            // or SQLServer, the event is not raised.
            if (this.Context.Session != null && this.Context != null)
            {
                string k = this.Session["TestObject"].ToString();
            }
            
        }





我甚至尝试过HttpContext.current.Session等..但没有工作。所有都抛出异常,因为会话结束在App启动时调用,甚至在会话超时时调用。



I even tried HttpContext.current.Session etc.. but none worked. All are throwing exception as Session end is called on App start up and even when session timed out.

推荐答案

这可能不是您的问题的答案,但请参阅下面的链接它解释了调用Session_End时同样奇怪的情况。



会话开始后立即触发ASP.NET会话结束事件 - 解决后立即触发[ ^ ]



查看会话状态是否发生相同情况。
This may not be answer for your question, but refer below link it explains about same weird scenario when Session_End get called.

ASP.NET Session End Event Fires Immediately After Session Start - Resolved[^]

See if the same is happening with your session state.


这篇关于为什么在App Start上调用Session End?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆