我将描述我的环境:我有Ninject + Ninject Interception Extension工作,为所有方法启用拦截器的自动注册,标记有特殊属性。这是一个常见的AoP +属性+ DI容器场景。
我的问题是: 当移植到最新版本的Ninject和Ninject Interception Extension - 3.0时,当我的拦截器应该运行时,我开始得到一个异常。我的InterceptorRegistrationStrategy在解析属性类型和注册拦截器时工作正常。但运行截获的方法会导致以下异常:
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 Interception Extension来解决这个问题,并且没有足够的文档使我处于不利的位置。
任何人在移植到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_");
}
}
答案 0 :(得分:3)
拦截的行为发生了变化:扩展将在注入接口而不是类代理时创建接口代理,因为这样做的优点是方法不再需要是虚拟的。您必须将其放入接口,从拦截中排除该方法(拦截无法调用的方法无用)或注入类而不是接口
答案 1 :(得分:1)
问题转移到项目的git hub:issue