IRequiresSessionState 与 IReadOnlySessionState

发布于 2024-12-14 06:28:33 字数 356 浏览 0 评论 0原文

除了无法保存对会话变量的更改之外,IRequiresSessionStateIReadOnlySessionState 之间还有什么区别?

两者都使我能够访问 HttpHandler 中的会话变量。但为什么我更喜欢IReadOnlySessionState?它只是限制我保存下一个请求的会话。
或者它是否给我带来了优于 IRequiresSessionState 的性能优势?

我什么时候更愿意使用 IReadOnlySessionState 而不是 IRequiresSessionState

What is the difference between IRequiresSessionState and IReadOnlySessionState beside the inability of the second to save changes to the session variables?

Both provide me the ability to access session variables in my HttpHandler. But why would I prefer IReadOnlySessionState? It just restricts me from saving the session for the next request.
Or does it gives me an performance advantage over IRequiresSessionState?

When would I prefer to use IReadOnlySessionState over IRequiresSessionState?

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

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

发布评论

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

评论(3

星星的轨迹 2024-12-21 06:28:33

一个关键的区别是 IRequiresSessionState 对当前会话设置了独占锁,从而可能限制当前用户的并发请求数。 (有关此锁定现象的更多背景信息,请参阅 使用 ASP.NET 会话时是否可以强制请求并发?

相比之下,IReadOnlySessionState 不会获取独占锁。

这与 renad 对几乎相同的 SO 问题的有用答案中记录的内容相同。

我找到的最好的官方文档来自 MSDN 文章 会话状态提供程序:

会话状态提供程序中最重要的三个方法是 GetItem、GetItemExclusive 和 SetAndReleaseItemExclusive。前两个由 SessionStateModule 调用以从数据源检索会话。如果请求的页面实现 IRequiresSessionState 接口(默认情况下,所有页面都实现 IRequiresSessionState),则 SessionStateModule 的 AcquireRequestState 事件处理程序将调用会话状态提供程序的 GetItemExclusive 方法。方法名称中的“Exclusive”一词意味着仅当会话当前未被其他请求使用时才应检索该会话。另一方面,如果请求的页面实现 IReadOnlySessionState 接口(实现此目的的最常见方法是在页面的 @ Page 指令中包含 EnableSessionState="ReadOnly" 属性),SessionStateModule 将调用提供程序的 GetItem 方法。这里不需要排他性,因为 SessionStateModule 允许重叠读取访问。

请注意显式使用这些接口与使用 之间的相似之处EnableSessionState 页面指令:

  • EnableSessionState=False <->没有 I*SessionState 接口
  • EnableSessionState=True <-> IRequiresSessionState接口
  • EnableSessionState=ReadOnly <-> IReadOnlySessionState

One critical difference is that IRequiresSessionState puts an exclusive lock on the current session, thereby potentially limiting the # of concurrent requests from the current user. (For more background on this locking phenomenon, see Is it possible to force request concurrency when using ASP.NET sessions?)

In contrast, IReadOnlySessionState does not acquire an exclusive lock.

This is the same thing documented in renad's helpful answer to an almost identical SO question.

The best official documentation I've found for this is from MSDN article Session State Providers:

Three of the most important methods in a session state provider are GetItem, GetItemExclusive, and SetAndReleaseItemExclusive. The first two are called by SessionStateModule to retrieve a session from the data source. If the requested page implements the IRequiresSessionState interface (by default, all pages implement IRequiresSessionState), SessionStateModule's AcquireRequestState event handler calls the session state provider's GetItemExclusive method. The word "Exclusive" in the method name means that the session should be retrieved only if it's not currently being used by another request. If, on the other hand, the requested page implements the IReadOnlySessionState interface (the most common way to achieve this is to include an EnableSessionState="ReadOnly" attribute in the page's @ Page directive), SessionStateModule calls the provider's GetItem method. No exclusivity is required here, because overlapping read accesses are permitted by SessionStateModule.

Note the parallel between explicitly using these interfaces and using the EnableSessionState Page directive:

  • EnableSessionState=False <-> no I*SessionState interface
  • EnableSessionState=True <-> IRequiresSessionState interface
  • EnableSessionState=ReadOnly <-> IReadOnlySessionState
栖迟 2024-12-21 06:28:33

该接口控制框架是否在请求结束时保存当前会话状态。当您使用进程外会话状态存储时,它会产生更大的差异。在这种情况下,如果没有该接口,系统仍会将会话数据存储在远程数据库中,即使它没有更改(系统不会跟踪会话数据在请求期间是否被修改)。当您使用 IReadOnlySessionState 接口时,会跳过写回阶段。

That interface controls whether the framework will save the current session state at the end of the request. It makes a bigger difference when you're using out-of-process session state storage. In that case, without the interface, the system will still store the session data in the remote database, even when it hasn't changed (the system doesn't keep track of whether the session data was modified during the request). When you use the IReadOnlySessionState interface, the write-back phase is skipped.

苦妄 2024-12-21 06:28:33

请遵循此 http://msdn.microsoft.com/en -us/library/system.web.sessionstate.irequiressessionstate.aspx

IRequiresSessionState 派生自 System.Web.SessionState
使用此接口,我们可以访问 Httphandler 和 Class 文件中的会话。

如果需要对会话进行只读访问,请实现 IReadOnlySessionState 接口。

Follow this http://msdn.microsoft.com/en-us/library/system.web.sessionstate.irequiressessionstate.aspx

IRequiresSessionState is derived from System.Web.SessionState
using this interface we access session in Httphandler and Class file

If you need read-only access to the Session, implement the IReadOnlySessionState interface.

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