禁用会话状态按预期工作:
[SessionState(SessionStateBehavior.disabled)] public class SampleController : Controller { public ActionResult test() { // Access to the session should be denied object test = Session["test"]; return Content(test); } }
到〜/ Sample / Test会抛出一个System.Web.HttpException异常。
但是,只读会话似乎有点奇怪:
[SessionState(SessionStateBehavior.ReadOnly)] public class SampleController : Controller { public ActionResult test() { // Read from the session should be fine object test = Session["test"]; return Content(test); } public ActionResult SetTest(string value) { // Write to the session should fail Session["test"] = value; // Read it back from the session object test = Session["test"]; return Content(test); } }
所以现在我期望〜/ Sample / Test工作,而且它。奇怪的是,集合也是:我去〜/ Sample / SetTest?value = foo,它不抛出异常,实际上它返回“foo”。如果我调用〜/ Sample / SetTest?value = bar,然后〜/ Sample / Test我得到“bar”,表示会话已经写入。
所以在SessionStateBehavior.ReadOnly我已经成功写入会话并读取我的值。
我认为这可能是由于三件事之一:
>在MVC 3 [SessionState(SessionStateBehavior.ReadOnly)]被打破/忽略。
>当会话被写入并变为可写时,[SessionState]被覆盖。
> SessionStateBehavior.ReadOnly实际上表示某种脏/乐观访问。
任何人都可以确认?
我怀疑最后一个是真的,基于custom session provider documentation – 如果这是怎么实现的工作?写入“只读”会话是否会产生并发错误(即最后写入胜利),还是会导致会话损坏和异常中断?
更新
看起来这是设计(从Microsoft’s docs):
Note that even if the EnableSessionState attribute is marked as
ReadOnly,other ASP.NET pages in the same application might be able to
write to the session store,so a request for read-only session data
from the store might still end up waiting for locked data to be freed.
看起来像上面的第二个选项是它实际上做的 – 会话被锁定和模式更改为可写。
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。