没有为此对象定义无参数构造函数。Ninject + MVC 5

San*_*wat 1 asp.net-mvc entity-framework dependency-injection ninject

我收到错误消息“没有为此对象定义无参数构造函数”。在MVC 5应用程序中。

错误

控制器是:

public class HomeController : Controller
    {
        private  IUserService userService;

        public HomeController(IUserService userService)
        {
            this.userService = userService;
        }

        [HttpGet]
        public ActionResult Index()
        {
            var user = userService.GetUser();
            return View();
        }
    }
Run Code Online (Sandbox Code Playgroud)

Ninject WebCOmmon CS类是:

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(QuestionWave.Web.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(QuestionWave.Web.App_Start.NinjectWebCommon), "Stop")]

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

    using Microsoft.Web.Infrastructure.DynamicModuleHelper;

    using Ninject;
    using Ninject.Web.Common;
    using QuestionWave.Data;
    using QuestionWave.Service;

    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)
        {
            kernel.Bind<QuestionWaveDbEntities>().ToSelf().InRequestScope();
            kernel.Bind(typeof(IRepository<>)).To(typeof(Repository<>)).InRequestScope();
            kernel.Bind<IUserService>().To<UserService>();
        }        
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经为Ninject安装了四个软件包

    <package id="Ninject" version="3.2.2.0" targetFramework="net45" />
  <package id="Ninject.MVC5" version="3.2.1.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" /> 
Run Code Online (Sandbox Code Playgroud)

我的DbContext类是:

public partial class QuestionWaveDbEntities : DbContext
{
    public QuestionWaveDbEntities()
        : base("name=QuestionWaveDbEntities")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

    public virtual DbSet<User> Users { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我正在使用使用EF的数据库优先方法。所有构造函数都是公共的。

我的代码出了什么问题,请提出建议。

Mik*_*ela 5

我用这种方式工作。创建Ninject依赖项解析器类:

public class NinjectDependencyResolver : IDependencyResolver
{
    private IKernel kernel;
    public NinjectDependencyResolver()
    {
        kernel = new StandardKernel();
        AddBindings();
    }

    public object GetService(Type serviceType)
    {
        return kernel.TryGet(serviceType);
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        return kernel.GetAll(serviceType);
    }

    private void AddBindings()
    {
       kernel.Bind<IUserService>().To<UserService>();
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在Global.asax Application_Start方法中设置依赖项解析器

DependencyResolver.SetResolver(new NinjectDependencyResolver());
Run Code Online (Sandbox Code Playgroud)