Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 660
cuongle Avatar asked Jun 07 '12 10:06

cuongle


People also ask

Does Net Web API support action filter?

Web API includes filters to add extra logic before or after action method executes. Filters can be used to provide cross-cutting features such as logging, exception handling, performance measurement, authentication and authorization.

How do I use action filter in Web API?

Action filters contain logic that is executed before and after a controller action executes. You can use an action filter, for instance, to modify the view data that a controller action returns. Result filters contain logic that is executed before and after a view result is executed.

Can we inject the dependency to individual action method of the controller?

You can take advantage of the ServiceFilter attribute to inject dependencies in your controller or your controller's action methods.

How do I create a custom filter in Web API?

You can create custom filter attributes by implementing an appropriate filter interface for which you want to create a custom filter and derive the FilterAttribute class to use that class as an attribute. For example, implement IExceptionFilter and the FilterAttribute class to create a custom exception filter.


1 Answers

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:

  • http://codeclimber.net.nz/archive/2009/02/10/how-to-use-ninject-to-inject-dependencies-into-asp.net-mvc.aspx
  • Ninject in an Action Filter

Autofac:

  • ASP.NET MVC 3, Action Filters, and Autofac Dependency Injection

**EDIT - additional option **

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

like image 142
AlexGad Avatar answered Sep 21 '22 14:09

AlexGad