4

Consider this code:

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

in this line:

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

I get this error:

Error loading Ninject component IAdviceFactory

No such component has been registered in the kernel's component container.

Suggestions:

1) If you have created a custom subclass for KernelBase, ensure that you have properly

 implemented the AddComponents() method.

2) Ensure that you have not removed the component from the container via a call to RemoveAll().

3) Ensure you have not accidentally created more than one kernel.

How can solve it?

4

1 回答 1

5

很可能您添加了Ninject.Extensions.Interception但没有添加任何具体实现Ninject.Extensions.Interception, DynamicProxy或Ninject.Extensions.Interception,LinFu

于 2013-08-30T14:42:39.660 回答