我遇到了一个好奇的ASP会话的行为。你可以强制控制器超出用户的会话 – 我想能够这样做,使多个请求可以同时执行,并使用会话使它们连续执行。
@H_301_2@禁用会话状态按预期工作:
但是,只读会话似乎有点奇怪:
>当会话被写入并变为可写时,[SessionState]被覆盖。
> SessionStateBehavior.ReadOnly实际上表示某种脏/乐观访问。 @H_301_2@任何人都可以确认? @H_301_2@我怀疑最后一个是真的,基于custom session provider documentation – 如果这是怎么实现的工作?写入“只读”会话是否会产生并发错误(即最后写入胜利),还是会导致会话损坏和异常中断? @H_301_2@更新 @H_301_2@看起来这是设计(从Microsoft’s docs):
[SessionState(SessionStateBehavior.Disabled)] public class SampleController : Controller { public ActionResult Test() { // Access to the session should be denied object test = Session["test"]; return Content(test); } }@H_301_2@到〜/ 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); } }@H_301_2@所以现在我期望〜/ Sample / Test工作,而且它。奇怪的是,集合也是:我去〜/ Sample / SetTest?value = foo,它不抛出异常,实际上它返回“foo”。如果我调用〜/ Sample / SetTest?value = bar,然后〜/ Sample / Test我得到“bar”,表示会话已经写入。 @H_301_2@所以在SessionStateBehavior.ReadOnly我已经成功写入会话并读取我的值。 @H_301_2@我认为这可能是由于三件事之一: @H_301_2@>在MVC 3 [SessionState(SessionStateBehavior.ReadOnly)]被打破/忽略。
>当会话被写入并变为可写时,[SessionState]被覆盖。
> SessionStateBehavior.ReadOnly实际上表示某种脏/乐观访问。 @H_301_2@任何人都可以确认? @H_301_2@我怀疑最后一个是真的,基于custom session provider documentation – 如果这是怎么实现的工作?写入“只读”会话是否会产生并发错误(即最后写入胜利),还是会导致会话损坏和异常中断? @H_301_2@更新 @H_301_2@看起来这是设计(从Microsoft’s docs):
@H_301_2@Note that even if the EnableSessionState attribute is marked as@H_301_2@看起来像上面的第二个选项是它实际上做的 – 会话被锁定和模式更改为可写。
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.