Ninject: No matching bindings are available, and the type is not self-bindable

那年仲夏 提交于 2019-12-13 02:09:06

问题


Edit: Problem solved

This is my error message:

Message=Error activating IValueCalculator No matching bindings are available, and the type is not self-bindable. Activation path:
1) Request for IValueCalculator

Suggestions:

1) Ensure that you have defined a binding for IValueCalculator.

2) If the binding was defined in a module, ensure that the module has been loaded into the kernel.

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

4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name.

5) If you are using automatic module loading, ensure the search path and filters are correct.

Thrown here:

IValueCalculator calc = ninjectKernel.Get<IValueCalculator>();

Here is my binding:

kernel.Bind<IValueCalculator>().To<LinqValueCalculator>();

I found this question on SO, but it did not help me(right?): Ninject WithConstructorArgument : No matching bindings are available, and the type is not self-bindable

Any idea?


回答1:


I similarly facing the same issue and i found that this is because the kernel instance you passing with bind method not initialized.

I put the code please reference it.

....
var kernel = new StandardKernel();
new NinjectDI(kernel).Load();
kernel.Load(Assembly.GetExecutingAssembly());
IValueCalculator calc = ninjectKernel.Get<IValueCalculator>();
...

class NinjectDI : NinjectModule
{
    StandardKernel kernel;
    public NinjectDI(StandardKernel kernel)
    {
        this.kernel = kernel;
    }
    public override void Load()
    {
        kernel.Bind<IValueCalculator>().To<LinqValueCalculator>();
    }
}

Note: Ensure you have not accidentally created more than one kernel.



来源:https://stackoverflow.com/questions/29733867/ninject-no-matching-bindings-are-available-and-the-type-is-not-self-bindable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!