写在MVC只读会话3+ [英] Writing to a read only session in MVC 3+

查看:174
本文介绍了写在MVC只读会话3+的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的ASP会话的好奇行为。可以强制控制器是用户的会话之外 - 我希望能够做到这一点,使多个请求可以同时执行并使用会话导致他们连续执行

I've come across a curious behaviour of ASP sessions. You can force a controller to be outside of the user's session - I want to be able to do this so that multiple requests can execute at the same time and using a session causes them to execute consecutively.

禁用会话状态按预期工作:

Disabling session state works as expected:

[SessionState(SessionStateBehavior.Disabled)]
public class SampleController : Controller
{
    public ActionResult Test() 
    {
        // Access to the session should be denied
        object test = Session["test"];
        return Content(test);
    }
}

要的〜/采样/测试的将抛出一个 System.Web.HttpException ,符合市场预期。
但是,只读会出现奇怪的行为一点:

Going to ~/Sample/Test will throw a System.Web.HttpException, as expected. However, read-only sessions appear to behave a little strangely:

[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);
    }
}

所以现在我期待的〜/采样/测试的工作,而它的作用。奇怪的一点是,一组做太:我去的〜/采样/ SetTest值= foo的的,并没有抛出异常,事实上它返回富?如果我打电话的〜/采样/ SetTest?值=栏的,然后的〜/采样/测试的我得到栏,表明会话已被写入。

So now I expect ~/Sample/Test to work, and it does. The odd bit is that the set does too: I go to ~/Sample/SetTest?value=foo and it doesn't throw an exception, in fact it returns "foo". If I call ~/Sample/SetTest?value=bar and then ~/Sample/Test I get "bar", indicating that the session has been written to.

因此​​,在 SessionStateBehavior.ReadOnly 我已经成功地写入了会议,并读我的价值了。

So in a SessionStateBehavior.ReadOnly I have successfully written to the session and read my value back.

我认为这可能是三种情况之一造成的:

I think this could be due to one of three things:


  • 在MVC 3 [SessionState会(SessionStateBehavior.ReadOnly)] 坏/忽略了。
  • [SessionState会] 被重写时,会被写入,并成为可写的。

  • SessionStateBehavior.ReadOnly 实际上表明某种肮脏/乐观的访问。

  • In MVC 3 [SessionState(SessionStateBehavior.ReadOnly)] is broken/ignored.
  • The [SessionState] is overridden when the session is written to and becomes writeable.
  • The SessionStateBehavior.ReadOnly actually indicates some kind of dirty/optimistic access.

谁能证实?

我怀疑最后一个是真实的,基于该自定义的会话提供程序文档 - 如果它是那么如何实施工作?是否写入只读会话风险并发错误(即最后写胜出),或者它的风险腐败会议,并打破异常?

I suspect the last one is true, based off the custom session provider documentation - if it is then how does the implementation work? Does writing to a 'read only' session risk concurrency errors (i.e. last write wins) or does it risk corrupt sessions and breaking exceptions?

更新

看起来这是由设计(从<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.sessionstate.sessionstatestoreproviderbase.aspx\">Microsoft's文档):

It looks like this is by design (from Microsoft's docs):

请注意,即使的EnableSessionState属性被标示为
  只读,在同一应用程序的其他ASP.NET页面可能可以
  写入会话存储,所以只读会话数据的请求
  从商店仍可能最终等待被释放锁定的数据。

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.

它看起来像上面的第二个选择是什么它实际上 - 会话被锁定,模式更改为可写

It looks like the second option above is what it actually does - the session is locked and the mode changed to writable.

推荐答案

〜/采样/ SetTest?值=富

~/Sample/SetTest?value=foo

是的,它不会引发任何错误,但它也没有在请求结束持续了会议。按照设计,你写信给出席会议的所有东西被更新(仅当会话是可写的),在请求生命周期的尽头。

Yes it won't throw any error, but it also didn't persist the session at the end of the request. By design any thing that you write to session gets updated (only if the session is writable) at the very end of the request life-cycle.

在我的测试中〜/采样/测试返回任何内容。

In my test ~/Sample/Test returns nothing.

我觉得当会话是只读的,他们竟然没有快速这里。

I think they should have failed fast here when the session is readonly.

根据您的样品需要重写的方式

By the way your sample need to be rewritten

string test = (string)this.Session["test"]; 

这篇关于写在MVC只读会话3+的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆