1

I am trying to get work with dependency injection into the action filter, but it seems not working

I've registered the type in UnityConfig

    public static void RegisterComponents(IUnityContainer container)
    {
        GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
        container.RegisterType<Domain.Contracts.IUserServices, Domain.UserServices>(new ContainerControlledLifetimeManager());
        ...
    }

The code is being called successfully, however, the instantiated object (IUserServices) is null.

public class ValidationActionFilter : ActionFilterAttribute
{
    [Dependency]
    public IUserServices userServices { get; set; }

    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        var isValidUser = userServices.IsAValidUserForCompany("MyCompany", 123);
        ....
     }
}

I have tried to use following link solution, but this does not seem working for me because it IUnityContainer is null for me always

Solution

First, I have created an IFilterProvider

public class UnityActionFilterProvider
         : ActionDescriptorFilterProvider, IFilterProvider
{
      private readonly IUnityContainer container;

      public UnityActionFilterProvider(IUnityContainer container)
      {
           this.container = container;
      }

      public new IEnumerable<FilterInfo> GetFilters(
                 HttpConfiguration configuration, 
                 HttpActionDescriptor actionDescriptor)
      {
           var filters = base.GetFilters(configuration, actionDescriptor);

           foreach (var filter in filters)
           {
             container.BuildUp(filter.Instance.GetType(), filter.Instance);
           }

           return filters;
       }
}

Then registered the IFilterProvider in global.asax

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        GlobalConfiguration.Configure(WebApiConfig.Register);
        UnityConfig.RegisterComponents(UnityConfig.GetConfiguredContainer());


        var providers = GlobalConfiguration.Configuration.Services.GetFilterProviders().ToList();

        GlobalConfiguration.Configuration.Services.Add(
            typeof(System.Web.Http.Filters.IFilterProvider),
            new UnityActionFilterProvider(UnityConfig.GetConfiguredContainer()));

        var defaultprovider = providers.First(p => p is ActionDescriptorFilterProvider);

        GlobalConfiguration.Configuration.Services.Remove(
            typeof(System.Web.Http.Filters.IFilterProvider),
            defaultprovider);
    }
}
7
  • Possible duplicate of Constructor Dependency Injection WebApi Attributes Commented Dec 29, 2017 at 8:47
  • What version of Unity are you using? Commented Dec 29, 2017 at 11:23
  • @RandyLevy I am using Unity 4.0.1 & Unity.WebAPI 5.2.3 Commented Dec 29, 2017 at 11:25
  • One quick check that I've seen cause issues before: you can verify that [Dependency] is actually of type Microsoft.Practices.Unity.DependencyAttribute and not another type such as System.Runtime.CompilerServices.DependencyAttribute. Commented Dec 29, 2017 at 13:05
  • @RandyLevy it is type of Microsoft.Practices.Unity.DependencyAttribute Commented Dec 29, 2017 at 17:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.