【问题标题】:Using interceptor for asp.net controller using Simple Injector使用 Simple Injector 为 asp.net 控制器使用拦截器
【发布时间】:2019-03-19 14:37:37
【问题描述】:

我正在尝试为我的所有控制器添加一个拦截器。我正在使用 Simple Injector 和 asp.net MVC。

在尝试此示例时:https://simpleinjector.readthedocs.io/en/latest/InterceptionExtensions.html 当我想将拦截器添加到接口时,一切正常。问题从控制器开始,因为它们是具体类型而不是接口......

private static void ThrowIfServiceTypeNotInterface(ExpressionBuiltEventArgs e)  {
    // NOTE: We can only handle interfaces, because
    // System.Runtime.Remoting.Proxies.RealProxy only supports interfaces.
    if (!e.RegisteredServiceType.IsInterface) {
        throw new NotSupportedException("Can't intercept type " +
                    e.RegisteredServiceType.Name + " because it is not an interface.");
    }
}

在上面的实现中,仅对接口有明确的限制,我想了解为什么?

有没有人有在 SimpleInjector 中为控制器/具体类型添加拦截器的经验?

【问题讨论】:

    标签: asp.net-mvc aop simple-injector


    【解决方案1】:

    在上面的实现中,仅对接口有明确的限制,我想了解为什么?

    嗯,评论已经回答了这个问题:

    // NOTE: We can only handle interfaces, because
    // System.Runtime.Remoting.Proxies.RealProxy only supports interfaces.
    

    为了能够应用拦截,您必须确保控制器由IController 接口解析。您将在此 q/a 中找到如何实现此目的:How to decorate an ASP.NET MVC controller with Simple Injector

    【讨论】:

      猜你喜欢
      • 2014-08-22
      • 2015-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多