InjectableFilterAttribute 永远不会命中 Filter

发布于 2024-08-15 20:20:53 字数 837 浏览 6 评论 0原文

在我的基本控制器上,我放置了 Logger 属性。 此 LoggerAttribute 如下所示:

public class LoggerAttribute: InjectableFilterAttribute
{
    public override Type FilterType
    {
        get { return typeof (LoggerActionFilter); }
    }
}

此 loggerattribute 上的 ctor 被命中,但 FilterType getter 未被命中。

过滤器本身的相关部分如下所示:

public class LoggerActionFilter: IActionFilter
{
    private readonly ILoggerService logger;

    public LoggerActionFilter (ILoggerService logger)
    {
        this.logger = logger;
    }
    <IActionFilter Implementeation>
}

过滤器的构造函数也永远不会受到影响。

对于我的服务的接线和服务定位器的实例化,请检查此处
ILoggerService 的注册可以在此处找到,

我缺少什么?

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 filter'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 here

What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风为裳 2024-08-22 20:20:53

如果为应用程序配置了正确的 ControllerFactory,这应该会自动工作。

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

请注意,如果您使用 DI 容器(Turbine 术语中的服务定位器)注册自定义 IControllerFactory,则将使用此 IControllerFactory 类型,并且如果它不是从 TurbineControllerFactory 派生,则不会将 TurbineActionInvoker 的实例分配给创建的 Controller - 这再次意味着您的 InjectableFilterAttribute 永远不会被调用。

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

作为示例,以下是 Turbine 配置的 Global.asax 的全部内容:

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

但是,请注意,没有任何 Global.asax.cs。

MyApplication 类必须从 TurbineApplication 派生并正确配置 DI 容器。这是一种实现方法:

public class MyApplication : TurbineApplication
{
    static MyApplication()
    {
        ServiceLocatorManager.SetLocatorProvider(() => new WindsorServiceLocator());
    }
}

显然,如果您使用不同的 DI 容器,则可以将 WindsorServiceLocator 替换为另一个 DI 容器。

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.

情独悲 2024-08-22 20:20:53

我有几个问题想问您:

  • 您使用的是哪个版本的 MVC? v1还是v2?如果 v2 是 RC 还是 beta2?
  • 您是否看过过滤器注入示例

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?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文