Zápisník Josefa Rouska

Python a React.js programátor, zaměřený na Shopify integrace

Injecting dependecies into Web API Action Filters

When I was working with ASP.NET Web API I needed to inject dependencies to ActionFilters using Ninject. This is solved by Ninject.Web.WebApi, but I had some stability issues with this library. When I started to dig a little deeper I found that in fact I’m using only one class from this library. Which is DefaultFilterProvider this class steps into play before filter methods are executed and injects dependencies into it.

public IEnumerable<FilterInfo> GetFilters(HttpConfiguration configuration,
                                        HttpActionDescriptor actionDescriptor)
{
    var filters = this.filterProviders.SelectMany(fp => 
            fp.GetFilters(configuration, actionDescriptor)).ToList();
    
    foreach (var filter in filters)
    {
        this.kernel.Inject(filter.Instance);
    }
    
    return filters;
}

And how do we tell the WEB API that we want to use this provider? First we need to register our provider to GlobalConfiguration services and then remove the default one.

var providers = GlobalConfiguration.Configuration.Services.GetFilterProviders().ToList();
GlobalConfiguration.Configuration.Services.Add(typeof (IFilterProvider),
                                               new DefaultFilterProvider(ninjectKernel, providers));
            
var defaultprovider = providers.First(i => i is ActionDescriptorFilterProvider);
GlobalConfiguration.Configuration.Services.Remove(typeof(IFilterProvider), defaultprovider);

Example is available on GitHub.