【问题标题】:Property Injection using Autofac使用 Autofac 进行属性注入
【发布时间】:2012-08-07 18:37:41
【问题描述】:

所以我有一个简单的问题(我认为)。

如何使用 Autofac 对 FilterAttribute 进行属性注入?

Public Class TestFilterAttribute
    Inherits ActionFilterAttribute

    Public Property Service As IMyService        

    Public Overrides Sub OnActionExecuting(filterContext As System.Web.Mvc.ActionExecutingContext)

        ** I need to get to the IMyService here **
        Dim val = Service.GetValue()

        MyBase.OnActionExecuting(filterContext)
    End Sub
End Class

我已经在容器中注册了服务:

builder.RegisterType(Of MyService).As(Of IMyService)().InstancePerHttpRequest()

或者也许我的做法是错误的。我希望能够注入过滤器属性。

另外,当使用Register时,你是否也必须使用.As?比如RegisterType

【问题讨论】:

    标签: asp.net-mvc-3 autofac property-injection


    【解决方案1】:

    确保您在容器构建器上调用 RegisterFilterProvider 方法,它会处理其余的工作。

    http://code.google.com/p/autofac/wiki/Mvc3Integration#Filter_Attribute_Property_Injection

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-26
      • 1970-01-01
      • 2012-09-21
      相关资源
      最近更新 更多