SessionStateModule“结束";事件未触发 [英] SessionStateModule "End" event not firing

查看:70
本文介绍了SessionStateModule“结束";事件未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在获取Web应用程序的 SessionStateModule 对象,以便在会话的开始和结束时附加代码.我无法将代码放在Global.asax中,因为我的代码打算成为每个Web项目的通用代码.

Hi,

I''m getting the SessionStateModule object of my web aplication to append code in the start and the end of sessions. I can''t put my code in Global.asax because my code intend to be a generic for every web projects.

public static void TurnOn() 
{ 
    var s = (SessionStateModule)HttpContext.Current.ApplicationInstance.Modules["Session"];
    s.End += new EventHandler(s_End); 
    s.Start += new EventHandler(s_Start); 
} 
 
private static void s_Start(object sender, EventArgs e) 
{ 
    //foo 
    //bar 
} 
 
private static void s_End(object sender, EventArgs e) 
{ 
    //foo 
    //bar 
}



Start 事件运行得很好,但是 End 事件在会话超时结束时未触发.是什么问题?



The Start event runs perfect, but the End event is not firing at the end of session timeout. What is the problem?

推荐答案

请参阅我对问题的评论.会话结束时可能没有测试用例.您需要放弃会话,或者等到Timeout属性指定的时间间隔过后,直到会话过期.如果查看此事件的MSDN帮助页面,则会找到结束会话的说明:
http://msdn.microsoft. com/en-us/library/system.web.sessionstate.sessionstatemodule.end%28v = vs.100%29.aspx [ http://msdn.microsoft. com/en-us/library/system.web.sessionstate.httpsessionstate.abandon%28v = vs.100%29.aspx [ http://msdn.microsoft. com/en-us/library/system.web.sessionstate.httpsessionstate.timeout%28v = vs.100%29.aspx [ http://msdn.microsoft. com/en-us/library/system.web.sessionstate.httpsessionstate.mode%28v = vs.100%29.aspx [
Please see my comment to the question. Probably you did not have a test case when a session has ended. You would need to abandon the session or wait until it is expired after some time interval specifies by the Timeout property. If you look at the MSDN help page for this event, you will find the explanation of ending a session:
http://msdn.microsoft.com/en-us/library/system.web.sessionstate.sessionstatemodule.end%28v=vs.100%29.aspx[^].

See also:
http://msdn.microsoft.com/en-us/library/system.web.sessionstate.httpsessionstate.abandon%28v=vs.100%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.web.sessionstate.httpsessionstate.timeout%28v=vs.100%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.web.sessionstate.httpsessionstate.mode%28v=vs.100%29.aspx[^].

Just in case: if by some reason you think that by closing a Web page in the browser the session will end right away, this is not so.

—SA


我自己解决了这个问题:

I''ve solved this myself:

public static void TurnOn()
{
    //Session End
    var webAssembly = Assembly.GetAssembly(typeof(HttpApplication));
    var t = webAssembly.GetType("System.Web.HttpApplicationFactory", true);
    var fieldtheApplicationFactory = t.GetField("_theApplicationFactory", BindingFlags.NonPublic | BindingFlags.Static);
    var fieldEnd = t.GetField("_sessionOnEndMethod", BindingFlags.NonPublic | BindingFlags.Instance);
    var appFactory = fieldtheApplicationFactory.GetValue(null);


    lock (appFactory)
    {
        fieldEnd.SetValue(appFactory, typeof(not_relevant).GetMethod("s_End", BindingFlags.NonPublic | BindingFlags.Static));
    }
}

private static void s_End(object sender, EventArgs e)
{
    //To-do: Preserve the original Session_End from Global.asax

    //foo
    //bar
}


这篇关于SessionStateModule“结束";事件未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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