【问题标题】:No matching bindings are available, and the type is not self-bindable in Ninject没有匹配的绑定可用,并且类型在 Ninject 中不可自绑定
【发布时间】:2013-01-28 05:30:34
【问题描述】:

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

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

我的绑定有什么问题?

激活 DbConnection 时出错

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

激活路径:

4) 将依赖 DbConnection 注入参数 DbContext 类型构造函数的现有连接

3) 将依赖 DbContext 注入到参数 dbContext 中 GenericRepository{User}

类型的构造函数

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

1) 请求 HomeController

建议:

1) 确保您已为 DbConnection 定义绑定。

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

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

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

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; }
    }

【问题讨论】:

    标签: c# ninject ninject.web.mvc


    【解决方案1】:

    Ninjects 寻找构造函数 in the following order:

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

    在您的情况下,您的 TLPContext 构造函数未标记为 [Inject],因此适用 2. 规则,Ninject 将尝试解析 base class contructor,然后引发异常。

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

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

    或者您可以在注册TLPContext 时使用ToConstructor 方法specify the constructor

    kernel.Bind<TLPContext>().ToConstructor(_ => new TLPContext());
    

    【讨论】:

    • 这很奇怪。我从未使用 .ToConstructor() 通过构造函数进行注入。我不想将 Ninject 引入我的 DataAccess 项目。我试过你的代码,它没有任何改变。但我上次忘记了:NinjectDependencyResolver.cs 源未找到我也得到了......任何线索?啊,现在我明白了。我以前只使用 WebApi 项目而不是 mvc。似乎 Ninject 在这里的工作方式不同......
    • “未找到 NinjectDependencyResolver.cs 源”是什么意思?您是如何使用 nuget 在您的项目中安装 Ninject 的?
    • 并且使用ToConstructor() 是否仍会收到完全相同的错误消息:“4)将依赖项 DbConnection 注入到 DbContext 类型的构造函数的参数 existingConnection 中”?您确定您的应用程序已正确重新编译。您可以尝试清理并重建您的解决方案吗?
    • 这很奇怪。现在我重建它并且 ToConstructor() 工作。 repo 被注入到 HomeController 中。但是当我在没有 ToConstructor 的情况下使用旧方法时,我得到了这个: BindingConfiguration.cs is not found ??
    • 很高兴您的原始问题得到解决。关于奇怪的错误:BindingConfiguration.cs 是Ninject source 的一部分。所以我不知道为什么 VS 抱怨它......你在使用 resharper 并使用它的源反编译功能吗?因为有时反编译的文件会混淆 VS。您可以尝试关闭所有打开的文件并重新打开解决方案。
    【解决方案2】:

    我曾经遇到过类似的问题。我正在使用Ninject MVC,我尝试使用新的StandardKernel ctor 实例化kernel,但没有成功。

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

    我改用bootstrapper.Kernel 解决了这个问题。

    【讨论】:

      猜你喜欢
      • 2015-06-26
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-01
      • 2015-01-07
      相关资源
      最近更新 更多