正在ActionFilterAttributes跨线程重用?这是如何工作的? [英] Are ActionFilterAttributes reused across threads? How does that work?

查看:90
本文介绍了正在ActionFilterAttributes跨线程重用?这是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在做一些测试与下面code尝试和锻炼ActionFilterAttributes是如何工作的:

I have been doing some testing with the following code to try and workout how ActionFilterAttributes work:

public class TestAttribute : ActionFilterAttribute
{
    private string _privateValue;
    public string PublicValue { get; set; }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        _privateValue = DateTime.Now.ToString();

        base.OnActionExecuting(filterContext);
    }
}

当我在两个并行的线程运行上面的code中的_privateValue场迷糊。然而,PublicValue属性不会混淆。

When I run the above code on two parallel threads the _privateValue field gets confused. However the PublicValue property doesn't get confused.

在我看来像ActionFilterAttributes跨线程重用,但新的实例是根据指定的公共属性的常量创建。我是不是正确的?

It looks to me like ActionFilterAttributes are reused across threads, but that new instances are created depending on the constants specified to public properties. Am I correct?

我在哪里可以找到这些信息?

Where can I find information on this?

推荐答案

这将依赖于ASP.NET MVC的版本,但你永远不应该实例的状态存储在将不同方法之间重用的行为过滤器。下面是示例从重大更改在ASP.NET MVC 3 的:

This will depend on the version of ASP.NET MVC but you should never store instance state in an action filter that will be reused between the different methods. Here's a quote for example from one of the breaking changes in ASP.NET MVC 3:

在ASP.NET MVC的previous版本,动作过滤器是每创建
  但在少数情况下要求。这种行为从来就不是一个保证
  行为而仅仅是一个实现细节和合同
  过滤器是考虑他们的无状态的。在ASP.NET MVC 3,过滤器
  缓存更积极。因此,任何自定义操作的过滤器,
  储存不当的实例状态可能被打破。

In previous versions of ASP.NET MVC, action filters are create per request except in a few cases. This behavior was never a guaranteed behavior but merely an implementation detail and the contract for filters was to consider them stateless. In ASP.NET MVC 3, filters are cached more aggressively. Therefore, any custom action filters which improperly store instance state might be broken.

这基本上意味着,动作过滤器的同一个实例可以重复使用于不同的动作,如果你有存储在它的实例状态下,它可能会打破。

This basically means that the same instance of the action filter can be reused for different actions and if you have stored instance state in it it will probably break.

和在$ C $来讲C此意味着你应该绝对不会写一个动作过滤器是这样的:

And in terms of code this means that you should absolutely never write an action filter like this:

public class TestAttribute : ActionFilterAttribute
{
    private string _privateValue;

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        _privateValue = ... some calculation
    }

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        // use _privateValue here
    }
}

但你应该把它写这样的:

but you should write it like this:

public class TestAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var privateValue = ... some calculation
        filterContext.HttpContext.Items["__private_value__"] = privateValue;
    }

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        var privateValue = filterContext.HttpContext.Items["__private_value__"];
        // use privateValue safely here
    }
}

这篇关于正在ActionFilterAttributes跨线程重用?这是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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