10

我正在使用 Ninjec、Ninject.Web.MVC 和 Ninject.Web.Common

当我启动我的 mvc 应用程序时,我收到此绑定错误:

我的绑定有什么问题?

激活 DbConnection 时出错

没有匹配的绑定可用,并且该类型不可自绑定。

激活路径:

4) 将依赖DbConnection注入到DbContext类型构造函数的参数existingConnection中

3) 将依赖 DbContext 注入 GenericRepository{User} 类型的构造函数的参数 dbContext

2) 将依赖 IGenericRepository{User} 注入 HomeController 类型的构造函数的参数 repo

1) 请求 HomeController

建议:

1) 确保您已经为 DbConnection 定义了一个绑定。

2) 如果绑定是在模块中定义的,请确保该模块已加载到内核中。

3) 确保您没有意外创建多个内核。

4) 如果您使用构造函数参数,请确保参数名称与构造函数参数名称匹配。

5) 如果您使用自动模块加载,请确保搜索路径和过滤器正确。

public interface IGenericRepository<T> where T : class
{
}

public class GenericRepository<T> : IGenericRepository<T> where T : class
{
        public GenericRepository(TLPContext dbContext)
        {
            DbContext = dbContext;
        }

        protected TLPContext DbContext { get; private set; }
}

[assembly: WebActivator.PreApplicationStartMethod(typeof(TLP.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(TLP.App_Start.NinjectWebCommon), "Stop")]

namespace TLP.App_Start
{
    using Microsoft.Web.Infrastructure.DynamicModuleHelper;
    using Ninject;
    using Ninject.Web.Common;
    using System;
    using System.Web;
    using TLP.DataAccess;
    using TLP.DataAccess.Contract;
    using TLP.DataAccess.Implementation;

    public static class NinjectWebCommon
    {
        private static readonly Bootstrapper bootstrapper = new Bootstrapper();
        public static void Start()
        {
            DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
            DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
            bootstrapper.Initialize(CreateKernel);
        }

        public static void Stop()
        {
            bootstrapper.ShutDown();
        }

        private static IKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
            kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
            kernel.Bind<TLPContext>();
            kernel.Bind(typeof(IGenericRepository<>)).To(typeof(GenericRepository<>));
            return kernel;
        }
    }
}


[DbModelBuilderVersion(DbModelBuilderVersion.V5_0)]
    public class TLPContext : DbContext
    {
        public TLPContext()
            : base("DefaultConnection")
        {
            // We do not want implicit uncontrollable lazy loading, instead we use the explicit Load method
            this.Configuration.LazyLoadingEnabled = false;
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

            // Primary key
            modelBuilder.Entity<User>().HasKey(p => p.UserId);
            modelBuilder.Entity<User>().Property(p => p.FirstName).HasMaxLength(30).IsRequired();
            modelBuilder.Entity<User>().Property(p => p.RegisteredAt).IsRequired();
        }

        public DbSet<User> Users { get; set; }
    }
4

2 回答 2

11

Ninjects按以下顺序查找构造函数:

  1. 标记为的构造函数[Inject]
  2. 参数最多的构造函数
  3. 默认构造函数

在您的情况下,您的TLPContext构造函数没有标记,[Inject]因此适用 2. 规则,Ninject 将尝试解析基类构造函数,然后抛出异常。

所以你可以通过用标记你的构造函数来解决这个问题InjectAttribute

[Inject]
public TLPContext()
   : base("DefaultConnection")
{
   this.Configuration.LazyLoadingEnabled = false;
}

或者您可以在注册时使用该方法指定构造函数ToConstructorTLPContext

kernel.Bind<TLPContext>().ToConstructor(_ => new TLPContext());
于 2013-02-12T20:18:06.160 回答
4

我曾经有过类似的问题。我正在使用Ninject MVC并尝试kernel使用新的StandardKernelctor来实例化它,但它没有用。

我的问题是@Elisa 前面提到的第 3 点:Ensure you have not accidentally created more than one kernel.

我通过使用解决了它bootstrapper.Kernel

于 2014-02-12T15:30:28.617 回答