尝试创建控制器时出现“Web API 2 Ninject”错误

时间:2016-01-25 20:16:31

标签: c# asp.net-web-api dependency-injection ninject asp.net-web-api2

加载我的控制器时出现此错误。

An error occurred when trying to create a controller of type 'MembersController'. Make sure that the controller has a parameterless public constructor.

我添加了

System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new Ninject.WebApi.DependencyResolver.NinjectDependencyResolver(kernel);
注册绑定后

NinjectWebCommon类。

我已经注册了相关服务。请参阅 编辑

 private static void RegisterServices(IKernel kernel)
 {
     kernel.Bind<IMembersService, MembersService>();
     kernel.Bind<MemberContext, MemberContext>();
 }        

以下是我添加的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" />
  <package id="Ninject.WebApi.DependencyResolver" version="0.1.4758.24814" targetFramework="net451" />

我错过了什么?

编辑

感谢我与Jerrod Horton的谈话(下文),我意识到我做错了绑定。

应该是

private static void RegisterServices(IKernel kernel)
{
    kernel.Bind<IMembersService>().To<MembersService>();
    kernel.Bind<MemberContext>().To<MemberContext>();
}

2 个答案:

答案 0 :(得分:3)

您的某个依赖项(或依赖项的依赖项)未完全向IoC容器注册。当web api尝试解析控制器时,如果它没有所需的每一个依赖关系,那么它将失败并且如果你的IoC容器试图解析ProductRepository但ProductRepository在其构造函数ISqlConnectionProvider中需要,那么你必须在你的IoC容器中注册ISqlConnectionProvider

我会从控制器开始。查看所有输入参数。确保每个都在IoC容器中注册。然后浏览每个类,并确保其中的任何依赖项都已注册(依此类推)。

这是我用于Ninject的代码。此代码存在于名为NinjectWebCommon.cs

的文件的App_Start文件夹中
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(YourProject.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(YourProject.App_Start.NinjectWebCommon), "Stop")]

namespace YourProject.App_Start
{
    using System;
    using System.Web;

    using Microsoft.Web.Infrastructure.DynamicModuleHelper;

    using Ninject;
    using Ninject.Web.Common;

    public static class NinjectWebCommon 
    {
        private static readonly Bootstrapper bootstrapper = new Bootstrapper();

        /// <summary>
        /// Starts the application
        /// </summary>
        public static void Start() 
        {
            DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
            DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
            bootstrapper.Initialize(CreateKernel);
        }

        /// <summary>
        /// Stops the application.
        /// </summary>
        public static void Stop()
        {
            bootstrapper.ShutDown();
        }

        /// <summary>
        /// Creates the kernel that will manage your application.
        /// </summary>
        /// <returns>The created kernel.</returns>
        private static IKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            try
            {
                kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
                kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

                RegisterServices(kernel);
                return kernel;
            }
            catch
            {
                kernel.Dispose();
                throw;
            }
        }

        /// <summary>
        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        private static void RegisterServices(IKernel kernel)
        {
        }        
    }
}

packages.config Ninject代码段

  <package id="Ninject" version="3.2.2.0" targetFramework="net45" />
  <package id="Ninject.Web.Common" version="3.2.3.0" targetFramework="net45" />
  <package id="Ninject.Web.Common.WebHost" version="3.2.3.0" targetFramework="net45" />
  <package id="Ninject.Web.WebApi" version="3.2.4.0" targetFramework="net45" />
  <package id="Ninject.Web.WebApi.WebHost" version="3.2.4.0" targetFramework="net45" />

packages.config web api和asp.net snippet

  <package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net45" />
  <package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net45" />
  <package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.HelpPage" version="5.2.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net45" />
  <package id="Microsoft.jQuery.Unobtrusive.Validation" version="3.2.0" targetFramework="net45" />
  <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />

答案 1 :(得分:2)

简短回答:

您需要安装所有这些软件包:

  • Ninject
  • Ninject.Extensions.Conventions
  • Ninject.Web.Common
  • Ninject.Web.Mvc
  • Ninject.Web.WebApi
  • Ninject.Web.WebApi.WebHost

在大多数情况下,缺少其中一个导致问题。