Ninject:每个被拦截的类实例有一个拦截器实例?
我目前遇到一个问题,尝试为每个被拦截的类实例连接一个拦截器实例。
我正在 InterceptorRegistrationStrategy 中创建 Advice 并设置回调以解析来自内核的拦截器(它有一个注入构造函数)。请注意,我只能在回调中实例化拦截器,因为 InterceptorRegistrationStrategy 没有引用 Kernel 本身。
IAdvice advice = this.AdviceFactory.Create(methodInfo);
advice.Callback = ((context) => context.Kernel.Get<MyInterceptor>());
this.AdviceRegistry.Register(advice);
我得到每个方法的拦截器实例。
有没有办法为每个被拦截的类型实例创建一个拦截器实例?
我正在考虑命名范围,但拦截类型和拦截器不互相引用。
I'm currently having a problem, trying to wire up exactly one interceptor instance per one instance of class being intercepted.
I'm creating and Advice in the InterceptorRegistrationStrategy and setting the callback to resolve an interceptor from the kernel (it has an injection constructor). Please note that I can only instantiate interceptor in the callback because InterceptorRegistrationStrategy does not have reference to Kernel itself.
IAdvice advice = this.AdviceFactory.Create(methodInfo);
advice.Callback = ((context) => context.Kernel.Get<MyInterceptor>());
this.AdviceRegistry.Register(advice);
I'm getting an instance of interceptor per method.
Is there any way to create one interceptor instance per type instance being intercepted?
I was thinking about Named Scope, but intercepted type and interceptor do not reference each other.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是不可能的,因为为绑定的所有实例的每个方法创建一个拦截器。
但是您可以做的不是直接在拦截器中执行拦截代码,而是获取将处理拦截的类的实例。
That's not possible as one single interceptor is created per method for all instances of a binding.
But what you can do is not to execute the interception code directly in the interceptor but to get an instance of a class that will handle the interception.
您是否尝试过使用 Fluent API 来配置您的拦截?
注意
Intercept()
扩展方法位于Ninject.Extensions.Interception.Infrastruct.Language.ExtensionsForIBindingSyntax
Have you tried using the fluent API to configure your interception?
N.B. The
Intercept()
extension method is inNinject.Extensions.Interception.Infrastructure.Language.ExtensionsForIBindingSyntax