Dependency Injection on the AuthorizationFilterAttribute

I'm new to Unity and IoC in general and as usual I quickly got into the binding ...

I created an Authorization Filter Attribute for the ASP.NET Web API Beta. Now I need to inject my authorizer into an attribute, but since it is an attribute, I can't just do it public TestAuthAttribute(IAuthorizer Authorizer)

in my constructor.

So, I decided to create a public property decorated with a [Dependency] attribute to inject properties, however it will not be allowed.

Here is the code:

public class TestAuthAttribute : AuthorizationFilterAttribute
{
    [Dependency]
    public IAuthorizer Authorizer { get; set; }

    public TestAuthAttribute() {
        ...
        }

    private bool authorizeCore(HttpRequestMessage request)
    {
        if (Authorizer == null)
            throw Error.ArgumentNull("Null Authorizer");  // <<<<< this is null
    }

      

When the controller is decorated with [TestAuth], the Attribute is fired, but the authorizer is not allowed, it is zero)

I put the following code in my controller and the authorizer allowed ...

[Dependency]
    public IAuthorizer Authorizer { get; set; }

      

Why is this dependency not resolved in my AuthorizationFilterAttribute and how would you go about it? Entering an authorizer into the AuthorizationFilterAttribute?

+3


source to share


2 answers


Full disclosure: I didn't use a turbine.

Having said that, I think this might solve your problem for you, or at least show you how to solve it.

They have a Unity Nuget package: http://nuget.org/packages/MvcTurbine.Unity



And you can find more details on their codeplex site here: http://mvcturbine.codeplex.com/

Hope it helps.

0


source


I am using Ninject for a similar purpose. I have a link to Ninject.Web.WebAPI that works great.



However, this doesn't seem to work with MVC4 RC.

-1


source







All Articles