为什么 Asp.Net 中的 Session 对象在超时后没有被删除?

发布于 2024-10-16 04:57:37 字数 612 浏览 3 评论 0原文

为什么超时后Session对象没有被删除?

我使用的是 Asp.Net 4.0,会话状态配置如下。

<sessionState mode="SQLServer" cookieless="false" timeout="5"
                  allowCustomSqlDatabase="true" 
                  sqlConnectionString="data source=.\SqlExpress;initial catalog=App_SessionState;user id=sa;password=xxxxxxxx"/>

如果我在浏览器中大约 10 分钟没有活动,是否应该删除 Session 对象。但 10 分钟后我仍然可以访问 Session 变量。我在这里错过了什么吗?

编辑:

如果我在 10 分钟后访问会话变量,如下所示,我不应该

  var myObj = Session["MyKey"] as MyClass;

在 10 分钟后得到 NULL mObj is not NULL 。

Why Session objects are not removed after Timeout period?

I am using Asp.Net 4.0 and Session state is configured as shown below.

<sessionState mode="SQLServer" cookieless="false" timeout="5"
                  allowCustomSqlDatabase="true" 
                  sqlConnectionString="data source=.\SqlExpress;initial catalog=App_SessionState;user id=sa;password=xxxxxxxx"/>

If I have not activity in browser for about 10 mins, shouldn't the Session object be removed. But after 10 mins I can still access the Session variable. Am I missing something here?

EDIT:

If I access a session variable after 10 mins as shown below shouldn't I get NULL

  var myObj = Session["MyKey"] as MyClass;

mObj is not NULL after 10 mins.

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

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

发布评论

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

评论(3

つ低調成傷 2024-10-23 04:57:38

安装了一个名为 DeleteExpiredSessions 的存储过程,从作业 ASPState_Job_DeleteExpiredSessions 调用,并且每分钟执行一次(如果我正确读取 InstallSqlState.sql 文件)。

该过程基本上调用 DELETE FROM ASPStateTempSessions WHERE Expires DELETE FROM ASPStateTempSessions WHERE Expires GETUTCDATE()

因此,如果对象未删除,请检查“过期”列,并验证您是否正在与 utc 日期进行比较。如果有疑问,请执行 SELECT * FROM ASPStateTempSessions WHERE Expires GETUTCDATE()。另外,请确保您的 ASPState_Job_DeleteExpiresSessions 已启用并正常工作。

一个快速的(并且完全未经证实的想法); SQL Server Express 附带 SQL 代理吗?它是否已启用并能够执行计划的作业?

There's a stored procedure installed called DeleteExpiredSessions, called from the job ASPState_Job_DeleteExpiredSessions, and is executed every minute (if I read the InstallSqlState.sql file correctly).

The procedure basically calls DELETE FROM ASPStateTempSessions WHERE Expires < GETUTCDATE()

So, if objects aren't removed, check the Expires column, and verify that you're comparing with the utc date. If in doubt, do a SELECT * FROM ASPStateTempSessions WHERE Expires < GETUTCDATE(). Also, make sure that your ASPState_Job_DeleteExpiresSessions is enabled and working.

A quick (and totally unconfirmed idea); do SQL Server Express come with the SQL Agent? Is it enabled and able execute scheduled jobs?

陌若浮生 2024-10-23 04:57:38

“session”永远不会是“null”,但是超时后,会话对象被清空(或重新实例化),另一个会话会自动启动(您可以通过处理 SessionEnd 和 SessionStart 事件来检查这一点),然后您将始终拥有对会话对象的引用。
不会发生吗?您还看到上一会话的数据吗?

The "session" is never "null", but after the timeout has expired, the session object is emptied (or re-instantiated), another session is automatically started (you can check this by handling SessionEnd and SessionStart events), and you will always have a reference to a session object.
Does not happen? Still you see previous session's data?

信愁 2024-10-23 04:57:38

补充一下 Simon 所说的内容,如果没有运行 SQL Server 代理,则不会清除会话值,除非实际执行数据库内的存储过程。
我认为 SQL Server Express 没有代理,因此不可能实现自动化作业。

如果您可以控制服务器,那么我建议通过窗口设置计划任务,该任务执行存储过程或清除过期会话的作业。我现在不知道存储过程的确切名称,但它的命名应该对其目的相当明显。

因此,您的选择是升级到具有可用 SQL Server 代理的 SQL Server 版本,或者进行设置以手动执行存储过程以清除过期会话。

或者,您可以使用 InProc 会话,该会话会自动清除。但我认为由于 InProc 是默认设置,因此您切换到 SQL Server 是有原因的。

To add onto what Simon said, If there is no SQL Server Agent running there will be no clearing of session values unless a stored procedure inside of the database is actually executed.
I don't think SQL Server express has the Agent so an automated job is not possible.

If you have control of the server then I would suggest setting up a scheduled task through windows that executes the stored procedure or job that clears your expired sessions. I don't know the exact name of the stored procedure right now but it should be named fairly obvious to it's purpose.

So your options are to upgrade to a version of SQL server that has the SQL Server Agent available or set something up to manually execute the stored procedure to clear expired sessions.

Alliteratively you can use InProc sessions, which are cleared automatically. But I assumed since InProc is the default there is a reason why you switched to SQL Server.

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