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

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

问题描述

我一直在使用以下代码进行一些测试,以尝试了解 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);
    }
}

当我在两个并行线程上运行上述代码时,_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 中,动作过滤器是根据请求,除了少数情况.这种行为从来没有保证行为,而只是一个实现细节和合同过滤器是将它们视为无状态的.在 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.

就代码而言,这意味着您绝对不应该编写这样的操作过滤器:

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
    }
}

但你应该这样写:

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天全站免登陆