2012-04-03 282 views
18

我一直在努力寻找这个我们的年龄。当我尝试我的类绑定与拦截我上线无法获得Ninject.Extensions.Interception工作

Kernel.Bind<MyClass>().ToSelf().Intercept().With<ILoggerAspect>(); 

错误加载Ninject组件IAdviceFactory收到以下异常。没有这样的组件已注册在内核中的组件容器

我已经试过与不LoadExtensions,大约用一个模块来建立我的绑定和我的最后一次尝试看起来像这样

internal class AppConfiguration 
{ 

    internal AppConfiguration() 
    { 
     var settings = new NinjectSettings() { LoadExtensions = false }; 
     Kernel = new StandardKernel(settings); 
     Load(); 
    } 

    internal StandardKernel Kernel { get; set; } 

    public static AppConfiguration Instance 
    { 
     get { return _instance ?? (_instance = new AppConfiguration()); } 
    } 

    private static AppConfiguration _instance; 

    private void Load() 
    { 
     Kernel.Bind<ILoggerAspect>().To<Log4NetAspect>().InSingletonScope(); 
     Kernel.Bind<MyClass>().ToSelf().Intercept().With<ILoggerAspect>(); 
    } 

    internal static StandardKernel Resolver() 
    { 
     return Instance.Kernel; 
    } 
} 

我的记录器属性看起来像这样

public class LogAttribute : InterceptAttribute 
{ 
    public override IInterceptor CreateInterceptor(IProxyRequest request) 
    { 
     return request.Context.Kernel.Get<ILoggerAspect>(); 
    } 
} 

我的截击这样

public class Log4NetAspect : SimpleInterceptor, ILoggerAspect 
{ 
    protected override void BeforeInvoke(IInvocation invocation) 
    { 
     Debug.WriteLine("Running " + invocation.ReturnValue); 
     base.BeforeInvoke(invocation); 
    } 

    public new void Intercept(IInvocation invocation) 
    { 
     try 
     { 
      base.Intercept(invocation); 
     } 
     catch (Exception e) 
     { 
      Debug.WriteLine("Exception: " + e.Message); 
     } 
    } 

    protected override void AfterInvoke(IInvocation invocation) 
    { 
     Debug.WriteLine("After Method"); 
     base.AfterInvoke(invocation); 
    } 
} 

回答

25

您很可能没有在应用程序[和Ninject.Extensions.Interception]旁边部署Ninject.Extensions.Interception.DynamicProxyNinject.Extensions.Interception.Linfu。你必须选择其中的一个。

使用现在的代码(LoadExtensions=false)它将无法获取特定的拦截库 - 您应该删除它,并且正常的扩展加载应该将扩展连接到内核上,捡起来。

+0

我试图用Nuget抓住他们,你需要两个还是只有一个? – 2012-04-03 07:53:03

+2

谢谢,就是这样。为什么nuget软件包没有添加其中的一个作为Default?从使用它的开发者那里,它没有什么区别。 – 2012-04-04 00:05:36

+0

有用的知道,我试图让它的工作使用LoadExtensions = false,并且无法从内核获取任何实例 – user1039513 2012-08-29 08:06:11

0

除了Remo Gloor's answer其中指出我朝着加入的NuGet包Ninject.Extensions.Interception.DynamicProxy,我一直得到相同的异常为OP,直到我手动加载的DynamicProxyModule - 在FuncModule手动加载,以及,要解决类似的错误涉及工厂扩展:

_kernel = new StandardKernel(
    new NinjectSettings{LoadExtensions = true}, 
    new FuncModule(), 
    new DynamicProxyModule()); // <~ this is what fixed it