我已经使用Ninject2一段时间了,并更新到Ninject3以更好地支持SignalR和其他框架。
下面的代码过去工作得很好。
public class AttributeDrivenPlanningStrategy<TAttribute, TInterceptor> : NinjectComponent, IPlanningStrategy
where TAttribute : Attribute
where TInterceptor : IInterceptor
{
private readonly IAdviceFactory adviceFactory;
private readonly IAdviceRegistry adviceRegistry;
public AttributeDrivenPlanningStrategy(IAdviceFactory adviceFactory, IAdviceRegistry adviceRegistry)
{
this.adviceFactory = adviceFactory;
this.adviceRegistry = adviceRegistry;
}
public void Execute(IPlan plan)
{
var methods = GetCandidateMethods(plan.Type);
foreach (var method in methods)
{
if(!method.HasAttribute<TAttribute>())
{ continue; }
var advice = adviceFactory.Create(method);
advice.Callback = request => request.Kernel.Get<TInterceptor>();
adviceRegistry.Register(advice);
if (!plan.Has<ProxyDirective>())
{ plan.Add(new ProxyDirective()); }
}
}
private static IEnumerable<MethodInfo> GetCandidateMethods(Type type)
{
const BindingFlags methodFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
var methods = type.GetMethods(methodFlags);
return methods.Where(ShouldIntercept);
}
private static bool ShouldIntercept(MethodInfo methodInfo)
{
return methodInfo.DeclaringType != typeof (object) &&
!methodInfo.IsPrivate &&
!methodInfo.IsFinal;
}
}
然而,现在它突然说它在ninject绑定中找不到IAdviceFactory,那么在最新版本的拦截器中是否有任何突破性的变化,因为几乎没有关于这个扩展的文档(考虑到这些天浮动的AoP数量,我觉得这很奇怪)。
那么,有人能给我指一个方向,告诉我问题是什么吗?
这是因为我没有安装拦截器的dynamicproxy-nuget包,因为ninject.interception的nuget包不包含其他库,因为您可以在基于linfu和castle的代理之间进行选择。