没有匹配的绑定可用,并且类型在Ninject中不可自绑定 [英] No matching bindings are available, and the type is not self-bindable in Ninject

查看:146
本文介绍了没有匹配的绑定可用,并且类型在Ninject中不可自绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using Ninjec, Ninject.Web.MVC and Ninject.Web.Common

启动我的mvc应用程序时,出现此绑定错误:

When I start my mvc application I get this binding error:

我的装订中有什么问题?

What do I wrong in my binding?

激活DbConnection时出错

Error activating DbConnection

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

No matching bindings are available, and the type is not self-bindable.

激活路径:

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

4) Injection of dependency DbConnection into parameter existingConnection of constructor of type DbContext

3)将依赖项DbContext注入参数的dbContext中 类型GenericRepository {User}

3) Injection of dependency DbContext into parameter dbContext of constructor of type GenericRepository{User}

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

2) Injection of dependency IGenericRepository{User} into parameter repo of constructor of type HomeController

1)请求HomeController

1) Request for HomeController

建议:

1)确保已为DbConnection定义了绑定.

1) Ensure that you have defined a binding for DbConnection.

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

2) If the binding was defined in a module, ensure that the module has been loaded into the kernel.

3)确保您没有意外地创建了多个内核.

3) Ensure you have not accidentally created more than one kernel.

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

4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name.

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

5) If you are using automatic module loading, ensure the search path and filters are correct.

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

推荐答案

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

Ninjects looks for constructors in the following order:

  1. 标记为[Inject]
  2. 的构造函数
  3. 参数最多的建设者
  4. 默认构造器
  1. Constructors marked with [Inject]
  2. Construtors with the most parameter
  3. Default contructor

在您的情况下,您的TLPContext构造函数未标记为[Inject],因此2.规则适用,Ninject将尝试解析

In your case your TLPContext constructor is not marked with [Inject] so the 2. rules applies and Ninject will try to resolve the base class contructor and then throws the exception.

因此,您可以通过用InjectAttribute

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

或者您可以通过ToConstructor方法指定构造函数注册TLPContext时:

Or you can specify the constructor with the ToConstructor method when registering your TLPContext:

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

这篇关于没有匹配的绑定可用,并且类型在Ninject中不可自绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆