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与您的应用程序 [and Ninject.Extensions.Interception] 一起部署。你必须选择其中之一。

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

于 2012-04-03T07:02:14.423 回答
3

除了Remo Gloor 的回答指出我要添加 nuget 包Ninject.Extensions.Interception.DynamicProxy之外,我一直得到与 OP 相同的异常,直到我手动加载 a DynamicProxyModule-FuncModule也是手动加载的,以解决涉及工厂扩展的类似错误:

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