ASP.NET MVC 3,动作过滤器和Autofac依赖注入 [英] ASP.NET MVC 3, Action Filters, and Autofac Dependency Injection

查看:334
本文介绍了ASP.NET MVC 3,动作过滤器和Autofac依赖注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP.NET MVC 2 我有一个 ActionFilterAttribute 名为 [交易] 即在执行操作前启动一个NHibernate的交易,提交或回滚回来之后,这取决于一个异常是否被抛出。在的Isession 实例的Htt prequestScoped() Autofac 。它看起来像这样伟大工程:

On ASP.NET MVC 2 I have an ActionFilterAttribute called [Transaction] that starts an NHibernate transaction before executing the action and commits or rolls it back afterward, depending on whether or not an exception was thrown. The ISession instance is HttpRequestScoped() and injected by Autofac. It looks like this and works great:

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
public sealed class TransactionAttribute : ActionFilterAttribute
{
    private ITransaction transaction;

    public TransactionAttribute()
    {
        this.Order = 0;
    }

    public ISession Session
    {
        get;
        set;
    }

    public override void OnActionExecuted(
        ActionExecutedContext filterContext)
    {
        if (this.Session != null && this.transaction != null)
        {
            try
            {
                if (this.transaction.IsActive)
                {
                    if (filterContext.Exception == null)
                    {
                        this.transaction.Commit();
                    }
                    else
                    {
                        this.transaction.Rollback();
                    }
                }
            }
            finally
            {
                this.transaction.Dispose();
                this.transaction = null;
            }
        }
    }

    public override void OnActionExecuting(
        ActionExecutingContext filterContext)
    {
        if (this.Session != null)
        {
            this.transaction = this.Session.BeginTransaction();
        }
    }
}

神奇。 <一href=\"http://weblogs.asp.net/srkirkland/archive/2009/09/03/asp-net-mvc-transaction-attribute-using-nhibernate.aspx\">Seems是一种常见的模式。

ASP.NET MVC 3 笔记,我看到在这个小Blurb的重大更改(重点煤矿):

In the ASP.NET MVC 3 notes, I see this little blurb under "Breaking Changes" (emphasis mine):

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

In previous versions of ASP.NET MVC, action filters were created 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.

哎呀。


  • 这是否意味着,如果我升级到MVC 3我在大清洗?

  • 如果动作过滤器不再​​每个请求实例化,我们将如何得到请求范围的依赖关系到我们的行动过滤器?

感谢您的任何见解。

推荐答案

我刚才问在谷歌论坛上类似的问题。
这里是链接<一href=\"https://groups.google.com/forum/#!topic/autofac/a0qqp2b3WA8\">https://groups.google.com/forum/#!topic/autofac/a0qqp2b3WA8

I just asked a similar question on google forums. Here is the link https://groups.google.com/forum/#!topic/autofac/a0qqp2b3WA8

我得到了答案:

builder.RegisterType<ExtensibleActionInvoker>().As<IActionInvoker>();


builder.RegisterControllers(Assembly.GetExecutingAssembly()).InjectActionInvoker();

然后你就可以在你的属性使用属性注入。

Then you can use property injection in your attributes.

这篇关于ASP.NET MVC 3,动作过滤器和Autofac依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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