Autofac不填充作用筛选器属性 [英] Autofac is not filling action filter property

查看:742
本文介绍了Autofac不填充作用筛选器属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在ASP.NET MVC 3应用程序,需要注射到它的一些相关性的动作过滤器。我使用Autofac.Mvc3作为依赖注入。

I have an action filter in an ASP.NET MVC 3 app that needs some dependencies injected into it. I am using Autofac.Mvc3 as the dependency injector.

根据的autofac维基我只是来注册类型的我要注入,调用 RegisterFilterProvider ,并把公共财产对我的行为过滤器,然后过滤器实例中autofac将填补与合适的对象属性。

According to the autofac wiki I just have to register the types that I want to inject, call RegisterFilterProvider, and put a public property on my action filter, and then autofac will fill the property with the right object during filter instantiation.

下面是我的行为过滤器的一部分:

Here is a part of my action filter:

Public Class LogActionAttribute
    Inherits ActionFilterAttribute

    Property tracer As TraceSource

    Public Overrides Sub OnActionExecuting(filterContext As System.Web.Mvc.ActionExecutingContext)
        ...
        tracer.TraceData(...)
        ...
    End Sub
End Class

下面是我的Global.asax的一部分:

Here is a part of my global.asax:

Public Class MvcApplication
    Inherits System.Web.HttpApplication

    Shared Sub RegisterGlobalFilters(ByVal filters As GlobalFilterCollection)
        filters.Add(New MyHandleErrorAttribute)
        filters.Add(New LogActionAttribute)
    End Sub

    Sub Application_Start()
        InitSettingRepoEtc()
        ...
    End Sub

    Protected Shared Sub InitSettingRepoEtc()
        ...
        Dim builder = New ContainerBuilder
        builder.RegisterControllers(Reflection.Assembly.GetExecutingAssembly)
        ...
        builder.Register(Of TraceSource)(
            Function(x) New TraceSource("requests", SourceLevels.All)).InstancePerHttpRequest()
        ...
        builder.RegisterFilterProvider()
        Dim container = builder.Build
        DependencyResolver.SetResolver(New AutofacDependencyResolver(container))
        ...
    End Sub
End Class

我已经把断点右后 SetResolver ,并在尝试直接窗口:

DependencyResolver.Current.GetService(Of TraceSource)

和我顺利地拿到了从autofac一个TraceSource对象,所以注册似乎是确定。

And I successfully got a TraceSource object from autofac, so the registration seems to be OK.

但在 OnActionExecuting 我的示踪剂属性为空。

我错过了什么?

推荐答案

IIRC提供者不具有全球性过滤器工作。

IIRC the provider doesn't work with 'global' filters.

删除这个功能:

Shared Sub RegisterGlobalFilters(ByVal filters As GlobalFilterCollection)
    filters.Add(New MyHandleErrorAttribute)
    filters.Add(New LogActionAttribute)
End Sub

和代替,注册Autofac直接全局过滤器:

And instead, register the global filters with Autofac directly:

 builder.Register(Of MyHandleErrorAttribute)
     .As(Of IActionFilter)
     .PropertiesAutowired()
     .SingleInstance();

 builder.Register(Of LogActionAttribute)
     .As(Of IActionFilter)
     .PropertiesAutowired()
     .SingleInstance();

Autofac将创建过滤器,并适当囊括其中。这种方法的好处是,你可以重构使过滤器不是属性衍生,然后用构造函数,而不是财产注入。

Autofac will create the filters and include them appropriately. The advantage of this approach is that you can refactor so the filters are not attribute-derived, and then use constructor rather than property injection.

这篇关于Autofac不填充作用筛选器属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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