簡體   English   中英

沒有匹配的綁定可用,並且該類型在Ninject中不可自綁定

[英]No matching bindings are available, and the type is not self-bindable in Ninject

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

當我啟動mvc應用程序時,出現此綁定錯誤:

我的裝訂有什么錯?

激活DbConnection時出錯

沒有匹配的綁定可用,並且類型不是自綁定的。

激活路徑:

4)將依賴項DbConnection注入類型DbContext的構造函數的參數existConnection中

3)將依賴關系DbContext注入GenericRepository {User}類型的構造函數的參數dbContext中

2)將依賴項IGenericRepository {User}注入到HomeController類型的構造函數的參數倉庫中

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

Ninjects 按以下順序查找構造函數:

  1. 標有[Inject]構造函數
  2. 參數最多的建設者
  3. 默認構造函數

在您的情況下,您的TLPContext構造函數未標記為[Inject]因此適用2.規則,Ninject將嘗試解析基類構造函數 ,然后引發異常。

因此,您可以通過使用InjectAttribute標記構造函數來解決此InjectAttribute

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

或者,您可以在注冊TLPContext時使用ToConstructor方法指定構造 TLPContext

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

我曾經有過類似的問題。 我正在使用Ninject MVC並且嘗試使用新的StandardKernel ctor實例化kernel ,但是它不起作用。

我的問題是@Elisa前面提到的第3點: Ensure you have not accidentally created more than one kernel.

我改用bootstrapper.Kernel解決了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM