How to do dependency injection to Action Filter on ASP.NET Web API
Asked Answered
A

3

22

I really get stuck on the approach to do dependency injection into action filter of web api. I have an action filter like this:

public class AuthorizationAttribute : ActionFilterAttribute
{
    public IApiKeyRepository Repository { get; set; }

    private Guid GetApiKey(string customerKey)
    {
        return Repository.GetApiKey(customerKey);
    }

    public override void OnActionExecuting(HttpActionContext actionContext)
    {        
    }
}

I would like to do property injection on the property Repository by using Windsor (but it does not matter which IoC container is used)

I did come up to customize FilterProvider but it did not work out for me, does anyone have solution or running code on this? it will be much appreciated

Additive answered 7/6, 2012 at 10:56 Comment(0)
B
4

You need to check your specific IOC implementation. IOC Containers such as NInject and Autofac have some type of filter injection by injecting public properties. Windsor I am unsure about, but here is a link that creates a wrapper which might help with Windsor: http://eagle081183.wordpress.com/2010/09/21/dependency-injection-with-asp-net-mvc-action-filters/ and another article directly addressing the issue with Windsor: http://weblogs.asp.net/psteele/archive/2009/11/04/using-windsor-to-inject-dependencies-into-asp-net-mvc-actionfilters.aspx.

For completeness with NInject and Autofac:

NInject:

Autofac:

**EDIT - additional option **

You should be able to do GlobalConfiguration.Configuration.DependencyResolver.GetService(...) from any filter regardless of the IOC container you are using.

Bandstand answered 7/6, 2012 at 13:22 Comment(2)
Thank you for your answer Alex, but most of the link you showed is for MVC, not for Web API, it is kind of different do injection between Web API and MVC, I think.Additive
I have done this successfully with Autofac but can't speak to the others. You should also be able to access the service you want directly from the Filter via GlobalConfiguration.Configuration.DependencyResolver.GetService(...). Not ideal, but it should work.Bandstand
T
4

For WebAPI and AutoFac you can use:

builder.RegisterWebApiFilterProvider(GlobalConfiguration.Configuration);

The documentation is here.

Turboprop answered 2/7, 2013 at 11:10 Comment(0)
N
1

You can use the Ninject.Web.WebApi assembly (obviously using Ninject as the IoC) to make this work. I would suggest looking at the source and SampleApplication via https://github.com/ninject/Ninject.Web.WebApi to see how they are accomplishing Filter injection.

Naturalize answered 7/6, 2012 at 12:43 Comment(2)
Could you outline the steps to use Ninject.Web.WebApi to accomplish filter injection? The SampleApplication isn't very helpful.Stroganoff
Here is a good outline of how the LogFilter in the SampleApplication is working - github.com/ninject/Ninject.Web.WebApi/wiki/…Naturalize

© 2022 - 2024 — McMap. All rights reserved.