I have Unity working perfectly for all the controllers in my ASP.NET Web API project - just using the default setting that exits the NuGet field. I also managed to connect it to the MVC Filter Attributes - but it doesn't seem to be able to do the same for the ASP.NET Web API filter attributes.
How to extend this default implementation to insert a dependency in an ActionFilterAttribute, for example ...
public class BasicAuthenticationAttribute : ActionFilterAttribute { [Dependency] public IMyService myService { get; set; } public BasicAuthenticationAttribute() { } }
This filter is applied to controllers using attributes:
[BasicAuthentication]
I'm sure I need to connect the Unity container so that it handles the creation of the attribute class, but it needs to know where to start, since it does not use the same extensibility points as MVC filters.
I just wanted to add, other things I tried include the location of the service, not the dependency injection, but the DependencyResolver you return is not the one you are setting.
// null var service = actionContext.Request.GetDependencyScope().GetService(typeof(IMyService));
or
// null var service = GlobalConfiguration.Configuration.DependencyResolver.GetService(typeof(IApiUserService));
source share