Ninject Interception - 移植到 Ninject 3.0 时的重大更改



我将描述我的环境:我有Ninject + Ninject拦截扩展,可以为所有方法启用拦截器的自动注册,并标有特殊属性。这是一个常见的 AoP + 属性 + DI 容器方案。

我的问题是:当移植到最新版本的 Ninject 和 Ninject 拦截扩展 - 3.0 时,当我的拦截器应该运行时,我开始出现异常。我的拦截器注册策略在解析属性化类型和注册拦截器时工作正常。但是运行截获的方法会导致以下异常:

System.ArgumentException : Interface not found.
at System.RuntimeTypeHandle.VerifyInterfaceIsImplemented(RuntimeTypeHandle handle, RuntimeTypeHandle interfaceHandle)
at System.RuntimeType.GetInterfaceMap(Type ifaceType)
at Ninject.Extensions.Interception.Advice.Advice.MatchesMethod(IProxyRequest request)
at System.Linq.Enumerable.WhereListIterator`1.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList(IEnumerable`1 source)
at Ninject.Extensions.Interception.Registry.AdviceRegistry.GetInterceptorsForRequest(IProxyRequest request)
at Ninject.Extensions.Interception.Registry.AdviceRegistry.GetInterceptors(IProxyRequest request)
at Ninject.Extensions.Interception.Wrapper.StandardWrapper.CreateInvocation(IProxyRequest request)
at Ninject.Extensions.Interception.Wrapper.DynamicProxyWrapper.Intercept(IInvocation castleInvocation)
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Infrastructure.Tests.Persistance.Conversations.NinjectConversationInterceptorBehavior.ShouldCreateInterceptorOnImplicitConversation() in NinjectConversationInterceptorBehavior.cs: line 74 

我不得不求助于 Reflector 并使用 Ninject 拦截扩展源来解决这个问题,再加上没有足够的文档,这让我处于不利地位。

有人在移植到 Ninject 3.0 时遇到同样的异常吗?

以下是我用来根据属性自动注册拦截器的代码:

public class NinjectConversationInterceptorRegistrationStrategy : InterceptorRegistrationStrategy
{
    public NinjectConversationInterceptorRegistrationStrategy(IAdviceFactory adviceFactory,
                                                              IAdviceRegistry adviceRegistry)
        : base(adviceFactory, adviceRegistry)
    {
    }
    public override void Execute(IPlan plan)
    {
        var pcAttribute = plan.Type.GetOneAttribute<PersistenceConversationalAttribute>();
        if (pcAttribute != null)
        {
            if (pcAttribute.MethodsIncludeMode == MethodsIncludeMode.Implicit)
            {
                foreach (var mi in GetCandidateMethods(plan.Type))
                {
                    RegisterMethodInterceptors(plan.Type, mi);
                    if (!plan.Has<ProxyDirective>())
                    {
                        plan.Add(new ProxyDirective());
                    }
                }
            }
            else
            {
                foreach (
                    var mi in
                        GetCandidateMethods(plan.Type).Where(
                            mi => mi.HasAttribute<PersistenceConversationAttribute>()))
                {
                    if (!mi.IsVirtual)
                    {
                        throw new InvalidOperationException(
                            string.Format("[PersistentCoversation] attribute used on non-virtual method {0}.{1}",
                                          mi.DeclaringType.Name,
                                          mi.Name));
                    }
                    RegisterMethodInterceptors(plan.Type, mi);
                    if (!plan.Has<ProxyDirective>())
                    {
                        plan.Add(new ProxyDirective());
                    }
                }
            }
        }
    }
    protected virtual void RegisterMethodInterceptors(Type type, MethodInfo method)
    {
        IAdvice advice = this.AdviceFactory.Create(method);
        advice.Callback = GetIntercepor;
        this.AdviceRegistry.Register(advice);
    }
    protected virtual IInterceptor GetIntercepor(IProxyRequest arg)
    {
        var interceptor = new NinjectConversationLazyInterceptor(arg.Kernel);
        return interceptor;
    }
    protected override bool ShouldIntercept(MethodInfo methodInfo)
    {
        if (IsPropertySetter(methodInfo))
        {
            return false;
        }
        var ret = base.ShouldIntercept(methodInfo);
        return ret;
    }
    private static bool IsPropertySetter(MethodBase methodInfo)
    {
        return methodInfo.IsSpecialName && methodInfo.Name.StartsWith("set_");
    }
}

拦截的行为发生了变化:扩展将在注入接口时创建一个接口代理而不是类代理,因为这具有方法不再需要虚拟的优点。要么你必须把它放到接口上,从拦截中排除方法(拦截一个无论如何都不能调用的方法也是没有用的),要么注入一个类而不是一个接口

问题已移至项目的 git 中心:问题

相关内容

  • 没有找到相关文章

最新更新