无法获得Ninject.Extensions.Interception工作

Rya*_*ham 20 ninject ninject-interception

多年来我一直在努力想到这一点.当我尝试用拦截器绑定我的类时,我在行上得到以下异常

Kernel.Bind<MyClass>().ToSelf().Intercept().With<ILoggerAspect>();
Run Code Online (Sandbox Code Playgroud)

加载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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

而我的拦截器就像这样

 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

Rem*_*oor 30

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

使用现在的代码(LoadExtensions=false),它将无法获取特定的拦截库 - 您应该删除它,并且正常的扩展加载应该在创建时将扩展连接到内核以获取拦截位.

  • 谢谢就是这样.为什么nuget包只是添加其中一个作为默认值?从开发人员使用它,它使用它没有任何区别. (2认同)