Property Injection using Autofac

270 views Asked by At

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.

1

There are 1 answers

0
Alex Meyer-Gleaves On BEST ANSWER

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