我怎样才能在会话状态查看家居所有活动会话? [英] How can I view everything in Session State for all active Sessions?
问题描述
我想创建一个管理页面来显示我们的使用会话状态的不失控。
是否有可能检索所有活动会话的列表,如果是这样,我怎么能访问所有的会话数据的每个会话?
免责声明:我的只是的想出了这个实现,因为我认为这是一个有趣的 - 并且可以解决的 - 问题。因此,可能会出现一些问题或细节我已经忘了考虑。不过,如果你正在使用的是InProc 会话状态,这里有一个解决方案。
摘要:(如列表)创建应用程序级对象存储在的Application_Start
活动期间创建的应用程序的状态,并在每个在session_start
事件,以会话的引用添加到列表中。在 Session_End中
,将其删除。要通过会话列表检索所有活动会话和价值观,循环,然后通过每个会话密钥。
的Global.asax
无效的Application_Start(对象发件人,EventArgs的发送)
{
应用[activeSessions] =新System.Collections.Generic.List< HttpSessionState>();
}无效在session_start(对象发件人,EventArgs的发送)
{
VAR activeSessions =(System.Collections.Generic.List< HttpSessionState>)应用[activeSessions];
activeSessions.Add(this.Session);
}无效Session_End中(对象发件人,EventArgs的发送)
{
VAR activeSessions =(System.Collections.Generic.List< HttpSessionState>)应用[activeSessions];
activeSessions.Remove(this.Session);
}
SomePage.aspx页面
//添加一些会话测试
this.Session [someStr] = DateTime.Now.ToString(); //获取会话
VAR activeSessions =(列表< HttpSessionState>)应用[activeSessions];
的foreach(在activeSessions VAR会话)
{
的Response.Write(会话+ session.SessionID +< BR />中);
的foreach(在session.Keys字符串键)
{
的Response.Write(键+:+会话[键] +< BR />中);
}
的Response.Write(<小时/>中);
}
输出:(装载了第二个浏览器击中页面后)
会话sj0sa255uizwlu45zivyfg2m
someStr:2009年8月28日上午11时03分37秒
----
会议530b3sjtea22jm451p15u355
someStr:2009年8月28日上午11时03分43秒
----
块引用>I'd like to create an administrative page to show that our use of session state isn't getting out of hand.
Is it possible to retrieve a list of all active sessions, and if so, how can I access all of the session data in each session?
解决方案Disclaimer: I just came up with this implementation because I thought this was an interesting - and solvable - problem. As such, there may be some issues or details I've neglected to consider. Nevertheless, if you are using InProc session state, here's a solution.
Summary: Create an Application-level object (eg. a List) stored in Application state created during the
Application_Start
event, and on eachSession_Start
event, add a reference to the session to your list. OnSession_End
, remove it. To retrieve all the active sessions and values, loop through your list of sessions, then through the session keys of each.Global.asax
void Application_Start(object sender, EventArgs e) { Application["activeSessions"] = new System.Collections.Generic.List<HttpSessionState>(); } void Session_Start(object sender, EventArgs e) { var activeSessions = (System.Collections.Generic.List<HttpSessionState>)Application["activeSessions"]; activeSessions.Add(this.Session); } void Session_End(object sender, EventArgs e) { var activeSessions = (System.Collections.Generic.List<HttpSessionState>)Application["activeSessions"]; activeSessions.Remove(this.Session); }
SomePage.aspx
//add something to session for test this.Session["someStr"] = DateTime.Now.ToString(); //get sessions var activeSessions = (List<HttpSessionState>)Application["activeSessions"]; foreach (var session in activeSessions) { Response.Write("Session " + session.SessionID + "<br/>"); foreach (string key in session.Keys) { Response.Write(key + " : " + session[key] + "<br/>"); } Response.Write("<hr/>"); }
Output: (after loading up a second browser to hit the page)
Session sj0sa255uizwlu45zivyfg2m someStr : 8/28/2009 11:03:37 AM ---- Session 530b3sjtea22jm451p15u355 someStr : 8/28/2009 11:03:43 AM ----
这篇关于我怎样才能在会话状态查看家居所有活动会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!