Ely Lucas Logo < Ely . Lucas />
Back to blog

Global Filters and Dependency Injection in MVC3

August 18, 2011 Ely Lucas

I was having trouble getting my dependencies resolved in my global filters. Turns out global filters are not instantiated through the FilterAttributeFilterProvider, so any properties I had on them with a resolve attribute were not getting resolved.

My solution was to use Ninject to instantiate the global filter during the app startup, like so:

private static void RegisterGlobalFilters(GlobalFilterCollection filters, IKernel kernel)
{
    filters.Add(new HandleErrorAttribute());
    filters.Add(kernel.TryGet(typeof(UserProviderFilterAttribute)));
}

At first, I felt a little dirty doing this and thought it was a giant hack. But the more I thought about it, the more it made sense. Since global.asax is the area of responsibility for creating global filters, it seemed like an appropriate spot to have Ninject resolve any dependencies that those filters might need.

Do you have an alternative way to do this, or do you have any other thoughts? Please let me know!