如何在asp.net mvc中订阅SessionStateItemExpireCallback

发布于 2024-10-22 03:51:49 字数 217 浏览 1 评论 0原文

我对此进行了很多搜索,但找不到任何实际使用的特定示例。 MSDN 说了很多关于实现自定义会话状态提供程序的内容,但我不想这样做。任何人都可以建议我如何在会话过期时执行一些操作

用例: 会话超时 10 分钟。 用户打开页面并创建会话,应用程序将数据库中的用户标记为已登录。页面打开一分钟后,用户关闭浏览器,因此 9 分钟后会话将过期,应用程序应执行一些代码将用户标记为离线。

提前致谢!

I searched a lot about this but couldn't find any particular example of real usage. MSDN says a lot about implementing custom session state provider but I don't wanna do that. Could anyone advise how can I perform some actions when session expires

Usecase:
Session timeout 10 minutes.
User opens a page and session is created application marks user in database as logged in. One minute after page open user closes browser so in 9 minutes session will expire and app should execute some code to mark user as offline.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

氛圍 2024-10-29 03:51:49

您可以尝试订阅 Global.asax 中的 Session_End 事件:

protected void Session_End()
{
    ...
    // Remark: don't use Context here, it will be null as the
    // user might have closed his browser long ago => no request/response
    // You could get the corresponding Session.SessionID though
}

您应该注意,只有在使用 InProc 会话时才会触发此事件。对于 SqlServerStateServer 模式,永远不会调用此事件,因为在其相应会话状态提供程序的实现中,SetItemExpireCallback 方法只是返回 false引用

内置会话状态如何
提供者处理过期回调?
进程内会话状态提供者,
InProcSessionStateStore,存储
ASP.NET 中的会话内容
应用程序缓存并利用
缓存的滑动过期
具有使会话过期的功能
去指定的时间段
而不被访问。当
通过缓存通知提供者
删除会话的回调
缓存已过期,它会通知
会话状态模块,以及
SessionStateModule 触发 Session_End
事件。另外两个内置
提供者—OutOfProcSessionStateStore
和 SqlSessionStateStore — 不支持
过期回调。均返回
SetItemExpireCallback 中为 false。
OutOfProcSessionStateStore 使用
应用程序缓存来存储会话,
但由于会话数据存储在
远程进程(“状态服务器”
过程),提供者不会尝试
通知 SessionStateModule 当
会话过期。 SqlSessionStateStore
依赖 SQL Server 代理来
“清理”会话状态数据库
并清理过期的会话。拥有
代理人通知提供商有关
会话已过期,因此提供商
反过来,可以通知
SessionStateModule 会很棘手
确实要努力——尤其是在网络领域
农场。

You could try subscribing for the Session_End event in Global.asax:

protected void Session_End()
{
    ...
    // Remark: don't use Context here, it will be null as the
    // user might have closed his browser long ago => no request/response
    // You could get the corresponding Session.SessionID though
}

You should note that this event is only triggered if you are using a InProc session. With SqlServer and StateServer modes this event is never called simply because in the implementation of their corresponding session state providers the SetItemExpireCallback method simply return false. Quoting:

How do the built-in session state
providers handle expiration callbacks?
The in-process session state provider,
InProcSessionStateStore, stores
session content in the ASP.NET
application cache and takes advantage
of the cache's sliding-expiration
feature to expire sessions when they
go for a specified period of time
without being accessed. When the
provider is notified via a cache
removal callback that a session
expired from the cache, it notifies
SessionStateModule, and
SessionStateModule fires a Session_End
event. The other two built-in
providers—OutOfProcSessionStateStore
and SqlSessionStateStore—don't support
expiration callbacks. Both return
false from SetItemExpireCallback.
OutOfProcSessionStateStore uses the
application cache to store sessions,
but since session data is stored in a
remote process (the "state server"
process), the provider doesn't attempt
to notify SessionStateModule when a
session expires. SqlSessionStateStore
relies on a SQL Server agent to
"scavenge" the session state database
and clean up expired sessions. Having
the agent notify the provider about
expired sessions so the provider
could, in turn, notify
SessionStateModule would be a tricky
endeavor indeed-especially in a Web
farm.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文