asp.net-mvc-3autofacproperty-injection

Property Injection using Autofac


So I have a simple question (I think).

How do you do Property Injection on a FilterAttribute with Autofac?

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

I have registered the service in the container:

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

Or maybe I am going about it the wrong way. I want to be able to inject filter attributes.

Also, when using Register, do you have to do .As as well? Like in RegisterType.


Solution

  • Make sure that you call the RegisterFilterProvider method on the container builder and it will take care of the rest.

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