没有这样的组件已在内核的组件容器中注册

时间:2013-08-26 07:11:17

标签: c# ninject aop

考虑以下代码:

public class IocConfig
{
    protected static StandardKernel Kernel { get; set; }

    public static void RegisterIoc(HttpConfiguration config)
    {
        using (IKernel kernel = new StandardKernel())
        {
            RegisterDependency();
            config.DependencyResolver = new NinjectDependencyResolver(kernel);
        }

    }

    public static void RegisterIoc()
    {

        RegisterDependency();
    }

    private static void RegisterDependency()
    {
        if (Kernel == null)
        {
            Kernel = new StandardKernel();
        }
        Kernel.Bind<CallCenterLogger>().ToSelf().Intercept().With(new TimingInterceptor());
    }

    public static T GetType<T>()
    {
        RegisterDependency();
        return Kernel.Get<T>();
    }
}

在这一行:

        Kernel.Bind<CallCenterLogger>().ToSelf().Intercept().With(new TimingInterceptor());

我收到此错误:

加载Ninject组件IAdviceFactory

时出错

内核的组件容器中没有注册此类组件。

建议:

1)如果您已为KernelBase创建了自定义子类,请确保您具有正确的

 implemented the AddComponents() method.

2)确保您没有通过调用RemoveAll()从容器中删除组件。

3)确保您没有意外创建多个内核。

如何解决?

1 个答案:

答案 0 :(得分:4)

很可能你已经添加了 Ninject.Extensions.Interception ,但没有添加任何具体的实现 Ninject.Extensions.Interception,DynamicProxy Ninject.Extensions.Interception ,李林甫