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

北战南征 提交于 2019-12-04 03:08:48

Ninjects looks for constructors in the following order:

  1. Constructors marked with [Inject]
  2. Construtors with the most parameter
  3. Default contructor

In your case your TLPContext constructor is not marked with [Inject] so the 2. rules applies and Ninject will try to resolve the base class contructor and then throws the exception.

So you can solve this by marking your constructor with the InjectAttribute

[Inject]
public TLPContext()
   : base("DefaultConnection")
{
   this.Configuration.LazyLoadingEnabled = false;
}

Or you can specify the constructor with the ToConstructor method when registering your TLPContext:

kernel.Bind<TLPContext>().ToConstructor(_ => new TLPContext());

I used to have similar problem. I was using Ninject MVC and I tried to instantiate the kernel using the new StandardKernel ctor, and it did't work.

My problem was the point 3 that @Elisa mentioned earlier: Ensure you have not accidentally created more than one kernel.

I solved it by using bootstrapper.Kernel instead.

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