InjectableFilterAttribute never hits the Filter
On my base controller I have placed the Logger
attribute.
This LoggerAttribute looks like this:
public class LoggerAttribute: InjectableFilterAttribute
{
public override Type FilterType
{
get { return typeof (LoggerActionFilter); }
}
}
The ctor on this loggerattribute gets hit, but the FilterType getter not.
The relevant part of the filter itself looks like this:
public class LoggerActionFilter: IActionFilter
{
private readonly ILoggerService logger;
public LoggerActionFilter (ILoggerService logger)
{
this.logger = logger;
}
<IActionFilter Implementeation>
}
The fi开发者_开发问答lter's ctor never gets hit either.
For the wiring of my services and instantiation of servicelocator check here
The registration of the ILoggerService can be found hereWhat am I missing?
This ought to work automatically provided that the correct ControllerFactory is configured for the application.
As far as I can tell, this must be an instance of TurbineControllerFactory or a derived class. The TurbineControllerFactory sets up the TurbineActionInvoker which is responsible for locating the correct filters.
Note that if you register a custom IControllerFactory with your DI Container (Service Locator in Turbine terminology), this IControllerFactory type will be used instead, and if this doesn't derive from TurbineControllerFactory, it will not assign an instance of TurbineActionInvoker to the created Controller - which again means that your InjectableFilterAttribute is never invoked.
The intended way to configure a Turbine application is to define a custom application class that derives from TurbineApplication.
As an example, here's the entire contents of a Turbine-configured Global.asax:
<%@ Application Codebehind="Global.asax.cs" Inherits="MyApplication" Language="C#" %>
However, note that there isn't any Global.asax.cs.
The MyApplication class must derive from TurbineApplication and correctly configure the DI Container. Here's one way to do it:
public class MyApplication : TurbineApplication
{
static MyApplication()
{
ServiceLocatorManager.SetLocatorProvider(() => new WindsorServiceLocator());
}
}
Obviously, you can replace the WindsorServiceLocator with another DI Container if you use a different one.
I have a couple of questions for you:
- Which version of MVC are you using? v1 or v2? if v2 is it the RC or beta2?
- Have you seen the Filter Injection sample?
精彩评论