使用 Simple Injector 为 asp.net 控制器使用拦截器

Using interceptor for asp.net controller using Simple Injector

我正在尝试为我的所有控制器添加一个拦截器。我正在使用 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 的经验?

In the implementation above there is an explicit restriction to interfaces only, I would like to understand why?

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

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

为了能够应用拦截,您必须确保控制器由 IController 接口解析。您将在 q/a 中找到实现此目的的方法:.