Ninject error in WebAPI 2.1 - Make sure that the controller has a parameterless public constructor

后端 未结 10 2147
心在旅途
心在旅途 2020-12-31 00:39

I have the following packages and their dependencies installed in my WebAPI project:

Ninject.Web.WebApi Ninject.Web.WebApi.OwinHost

10条回答
  •  忘掉有多难
    2020-12-31 01:07

    As other answers have pointed out, the error message can be misleading. Ninject might be working fine, but it could be that some dependency cannot be instantiated for a variety of reasons, such as:

    • A missing binding for an indirect dependency: https://stackoverflow.com/a/36438210/2279059
    • A dependency does not have a default constructor and does not support dependency injection (e.g., missing [Inject] property).
    • A cyclical dependency between two services

    You do not see all details in the error returned by the server (which may be by design). If you attach the debugger, you may also not be able to catch the exception unless you disable "Just My Code" in the debugging settings. Then you may be able to catch an internal Ninject exception which may have all the information you need to understand the problem.

    Older versions of Ninject, or if you are not using Ninject.Web.WebApi.WebHost, also require that you register Ninject as a dependency resolver, as pointed out in https://stackoverflow.com/a/24196092/2279059.

提交回复
热议问题