2012-04-03 245 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