具有 Ninject 无参数构造函数的 Web API

Web API with Ninject Parameterless Constructor

当我遇到这个问题时,我正在观看来自 Pluralsight 的 tutorial: 搜索此站点并找到 (Simple Injector Unable to Inject Dependencies in Web API Controllers, Make sure that the controller has a parameterless public constructor error ) 个不同的解决方案,但它们都不起作用。 使用 WebApi 和 Ninject 时遇到同样的问题,作者在编写代码时没有做任何事情,不知道为什么我会遇到这个问题,如果有人解释原因以及解决方案(写什么和在哪里来写)。这是代码:

//NinjectWebCommon.cs
 private static void RegisterServices(IKernel kernel)
    {
        kernel.Bind<IRepository>().To<Repository>().InRequestScope(); //for making singleton
        kernel.Bind<WebAPIandEFContext>().To<WebAPIandEFContext>().InRequestScope();

    }   
//Order Controller
  public class OrderController : ApiController
{
    private IRepository _repo;


    public OrderController(IRepository repo) 
    {
        _repo = repo;
    }
//remaining code....
ERROR description
{"$id":"1","Message":"An error has occurred.","ExceptionMessage":"An error 

occurred when trying to create a controller of type 'OrderController'. Make sure that the controller has a parameterless public constructor.","ExceptionType":"System.InvalidOperationException","StackTrace":"   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)\r\n   at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request)\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()","InnerException":{"$id":"2","Message":"An error has occurred.","ExceptionMessage":"Type 'WebAPIandEF.Controllers.OrderController' does not have a default constructor","ExceptionType":"System.ArgumentException","StackTrace":"   at System.Linq.Expressions.Expression.New(Type type)\r\n   at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType)\r\n   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)\r\n   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)"}}

更新 Nuget 包后我遇到了同样的问题。 Ninject.Web.WebApi 版本 3.2.4 引入了一项重大更改,需要根据 release notes.

安装软件包 "Ninject.Web.WebApi.WebHost"

安装上述包后,我的控制器再次开始解析依赖关系。

我也遇到过这个问题。我有 Ninject 个软件包,它们现在正在运行。

<package id="Ninject" version="3.2.0.0" targetFramework="net451" />
<package id="Ninject.MVC5" version="3.2.1.0" targetFramework="net451" />
<package id="Ninject.Web.Common" version="3.2.0.0" targetFramework="net451" />
<package id="Ninject.Web.Common.WebHost" version="3.2.0.0" targetFramework="net451" />
<package id="Ninject.Web.WebApi" version="3.2.4.0" targetFramework="net451" />

您还必须确保 Global.asax 中的 Application_Stat() 中的启动方法顺序正确。我的看起来像这样:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    GlobalConfiguration.Configure(WebApiConfig.Register);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}

出现此错误的原因有多种,但一般的潜在问题通常是您在 Ninject 的设置中遗漏了某些内容作为控制器的依赖项解析器。

在我的例子中,所有 Ninject 设置都是从​​ Startup.cs class 调用的。事实证明它很好,我只是缺少项目中的 Microsoft.Owin.Host.SystemWeb 包,所以 Startup 代码实际上从未被调用过。呸!