2

I've setup Unity in my WebAPI project and I am getting the objects injected properly when using the DependencyAttribute in one of my ApiControllers. I'm now trying to use the exact same approach in an ActionFilterAttribute, but it is not resolving (resolves to null).

This resolves to null:

public class ValidateEntryAttribute : ActionFilterAttribute
{
    [Dependency]
    internal ApplicationUserManager UserManager { get; set; }
    // ...
}

where this resolves to an instance of the object:

public class LayoutController : BaseApiController
{
    [Dependency]
    internal ApplicationUserManager UserManager { get; set; }
}

and my UnityConfig looks like this:

container.RegisterType<IUserStore<ApplicationUser, int>, ApplicationUserStore>();
container.RegisterType<ApplicationUserManager>();

What am I missing?

1
  • Unity can not inject things in attributes, change your approach. Commented Oct 2, 2015 at 6:54

1 Answer 1

2

You could change your approach to designing filters to make them passive and then use true dependency injection (constructor injection) on action filters that are designed to read them, as shown in this IActionFilter example or in this AuthorizeAttribute example.

Alternatively, you could hack the framework as shown here to get property injection on action filter attributes. But then you are stuck with property injection and all of the downsides that come with it.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.