gpt4 book ai didi

c# - IRequiresSessionState 与 IReadOnlySessionState

转载 作者:IT王子 更新时间:2023-10-29 04:21:50 25 4
gpt4 key购买 nike

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

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

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

最佳答案

一个关键区别是 IRequiresSessionState 在当前 session 上放置了一个独占锁,从而潜在地限制了来自当前用户的并发请求数。 (有关此锁定现象的更多背景信息,请参阅 Is it possible to force request concurrency when using ASP.NET sessions?)

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

这与 renad's helpful answer to an almost identical SO question 中记录的内容相同.

我找到的最好的官方文档来自 MSDN 文章 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.

注意显式使用这些接口(interface)和使用 EnableSessionState 之间的相似之处页面指​​令:

  • EnableSessionState=False <-> 无 I*SessionState 接口(interface)
  • EnableSessionState=True <-> IRequiresSessionState 接口(interface)
  • EnableSessionState=ReadOnly <-> IReadOnlySessionState

关于c# - IRequiresSessionState 与 IReadOnlySessionState,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8039014/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com