2013-02-12 38 views
9

我使用Ninjec,Ninject.Web.MVC和Ninject.Web.Common沒有匹配的綁定是可用的,並且類型不是自綁定在Ninject

當我開始我的MVC應用程序,我得到這個綁定錯誤:

我的綁定有什麼問題?

Error activating DbConnection

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

Activation path:

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

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

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

1) Request for HomeController

Suggestions:

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

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

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

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

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

回答

11

Ninjects尋找構造in the following order

  1. 構造標記[Inject]
  2. Construtors最參數
  3. 默認構造器

在您的情況下,您的TLPContext構造函數沒有標記爲[Inject],因此適用了2.規則,Ninject會嘗試解析base class contructor,然後引發異常。

所以,你可以通過註冊您的TLPContext當與InjectAttribute

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

或者你可以specify the constructorToConstructor標記方法,構造函數解決這個問題:

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

這很奇怪。我從來沒有用過.ToConstructor()來通過構造函數來獲取注入。我不想將Ninject引入到我的DataAccess項目中。我試過你的代碼,它並沒有改變。但是,我上次忘了:NinjectDependencyResolver.cs源找不到我也得到...任何線索?啊,現在我明白了。我之前只使用WebApi項目而不是mvc。似乎Ninject在這裏以不同的方式工作... – Elisabeth 2013-02-12 20:40:22

+0

「NinjectDependencyResolver.cs源找不到」是什麼意思?你是如何在nuget的項目中安裝Ninject的? – nemesv 2013-02-12 20:43:13

+0

我總是使用nuget yes。沒有手動安裝hehe – Elisabeth 2013-02-12 20:44:12

2

我曾經有過類似的問題。我正在使用Ninject MVC,我嘗試使用新的StandardKernel ctor實例化kernel,但它沒有奏效。

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

我用bootstrapper.Kernel,而不是解決它。