Ninject Interceptor自定义PlanningStrategy无法在Ninject 3中运行

时间:2013-07-15 21:24:46

标签: c# dependency-injection ninject aop ninject-interception

我一直在使用Ninject 2并更新到Ninject 3以更好地支持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浮动的数量。)

那么有人能指出我会告诉我这个问题是什么吗?

1 个答案:

答案 0 :(得分:1)

这归结于我没有安装拦截器的dynamicproxy nuget包,因为ninject.interception的nuget包不包含其他库,因为您可以在linfu和基于城堡的代理之间进行选择。