无法让 Ninject.Extensions.Interception 工作 [英] Can't get Ninject.Extensions.Interception working

查看:93
本文介绍了无法让 Ninject.Extensions.Interception 工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

多年来我一直在努力弄清楚这是我们的.当我尝试将我的班级与拦截器绑定时,我在线上收到以下异常

I've been trying for ages to figure this our. when i try to bind my class with an interceptor i'm getting the following exception on the line

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

加载 Ninject 组件 IAdviceFactory 时出错.内核的组件容器中尚未注册此类组件

我尝试过使用和不使用 LoadExtensions,大约使用模块来设置我的绑定,我最后一次尝试看起来像这样

I've tried with and without LoadExtensions, With about with using a Module to set up my bindings and my last attempt looks like this

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

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

My Logger Attribute looks like this

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

推荐答案

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

Most likely you didn't deploy Ninject.Extensions.Interception.DynamicProxy or Ninject.Extensions.Interception.Linfu alongside your application [and Ninject.Extensions.Interception]. You have to pick exactly one of them.

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

With the code as you have it right now (LoadExtensions=false) it will fail to pick up the specific interception library - you should remove that and the normal extensions loading should wire the extension into the Kernel on creation for the interception bits to pick it up.

这篇关于无法让 Ninject.Extensions.Interception 工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆