InjectableFilterAttribute嗟过滤器 [英] InjectableFilterAttribute never hits the Filter

查看:149
本文介绍了InjectableFilterAttribute嗟过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的基本控制器我已经放在日志属性。
这LoggerAttribute看起来是这样的:

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); }
    }
}

在这个loggerattribute的构造函数被击中,但过滤式吸气剂没有。

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 filter's ctor never gets hit either.

有关我的服务的线路和服务定位检查的实例这里结果
该ILoggerService的注册可以发现这里

For the wiring of my services and instantiation of servicelocator check here
The registration of the ILoggerService can be found here

我是什么失踪?

推荐答案

这应该自动工作的提供的正确的ControllerFactory配置为应用程序。

This ought to work automatically provided that the correct ControllerFactory is configured for the application.

据我所知,这必须是TurbineControllerFactory的实例或派生类。所述TurbineControllerFactory设置了TurbineActionInvoker它负责定位正确的过滤器。

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.

请注意,如果你与你的DI容器(服务定位器涡轮术语)注册一个定制IControllerFactory,这IControllerFactory类型将被用来代替,如果这并不TurbineControllerFactory派生,它不会TurbineActionInvoker的一个实例分配给创建控制器 - 这又意味着你InjectableFilterAttribute永远不会调用

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.

配置涡轮应用的预期方法是定义从TurbineApplication派生的自定义应用程序类。

The intended way to configure a Turbine application is to define a custom application class that derives from TurbineApplication.

作为一个例子,这里的的全部的涡轮配置的Global.asax内容:

As an example, here's the entire contents of a Turbine-configured Global.asax:

<%@ Application Codebehind="Global.asax.cs" Inherits="MyApplication" Language="C#" %>

不过,请注意,没有任何的Global.asax.cs。

However, note that there isn't any Global.asax.cs.

该MyApplication的类必须从TurbineApplication派生,并正确配置DI容器。下面是做这件事:

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());
    }
}

显然,你可以用另一个DI容器更换WindsorServiceLocator如果你使用一个不同的。

Obviously, you can replace the WindsorServiceLocator with another DI Container if you use a different one.

这篇关于InjectableFilterAttribute嗟过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆