20

私はこれを理解しようと何年も努力してきました。クラスをインターセプターにバインドしようとすると、行で次の例外が発生します

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);
    }
}
4

2 に答える 2

30

ほとんどの場合、アプリケーションを展開していないNinject.Extensions.Interception.DynamicProxyNinject.Extensions.Interception.Linfu、アプリケーションと一緒に展開していませんNinject.Extensions.Interception。それらの 1 つを正確に選択する必要があります。

現在のコード ( LoadExtensions=false) では、特定の傍受ライブラリを取得できません。これを削除する必要があります。通常の拡張機能の読み込みでは、傍受ビットがそれを取得するために、作成時に拡張機能をカーネルに接続する必要があります。

于 2012-04-03T07:02:14.423 に答える
3

の nuget パッケージを追加するように指示したRemo Gloor の回答Ninject.Extensions.Interception.DynamicProxyに加えて、手動で a をロードするまで、OP と同じ例外が発生し続けましたDynamicProxyModule- これFuncModuleも手動でロードされ、ファクトリ拡張に関する同様のエラーを回避します。

_kernel = new StandardKernel(
    new NinjectSettings{LoadExtensions = true}, 
    new FuncModule(), 
    new DynamicProxyModule()); // <~ this is what fixed it
于 2016-06-27T16:48:29.680 に答える