我已经连接了 MVC 4 项目和 Ninject 3。
现在我想处理我的 MVC 控制器方法的拦截。
如果我添加这个:
kernel.Bind<TT.Controllers.HomeController>().ToSelf().Intercept().With<TT.Interceptors.LoggingInterceptor>();
它有点工作(即使我自己的方法没有被拦截,而是我得到了从 Controller 基类拦截的 BeginExecute、EndExecute 和 Dispose 方法)。但是,让我们说现在还可以。
如果我想像这样在 HomeController 上拦截特定方法:
kernel.InterceptAround<TT.Controllers.HomeController>(
c => c.Index(),
invocation => doSomethingOnEnter(invocation),
invocation => doSomethingOnExit(invocation)
);
它根本行不通。拦截永远不会被解雇。
另一方面,如果我在项目中的某个普通服务类上使用相同的方法拦截,那么它就可以工作。似乎只有 Controller 方法有被拦截的问题。
kernel.InterceptAround<UrlService>(
c => c.DoSomething(),
invocation => doSomethingOnEnter(invocation),
invocation => doSomethingOnExit(invocation)
);
^这有效。
有谁知道我该怎么做?
PS。我将 NinjectWebCommon 与 WebActivators 一起使用:
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(TT.NinjectWebCommon), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(TT.NinjectWebCommon), "Stop")]