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

前端 未结 2 923
渐次进展
渐次进展 2021-02-20 13:58

I am using Ninjec, Ninject.Web.MVC and Ninject.Web.Common

When I start my mvc application I get this binding error:

What do I wrong in my binding?

2条回答
  •  心在旅途
    2021-02-20 14:12

    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().ToConstructor(_ => new TLPContext());
    

提交回复
热议问题