2013-02-12 7 views
9

Ich bin mit Ninjec, Ninject.Web.MVC und Ninject.Web.Commonkeine passenden Bindungen sind vorhanden, und der Typ ist nicht selbst bindable in Ninject

Wenn ich meine MVC-Anwendung starten ich diese Bindung Fehler erhalten :

Was mache ich falsch in meiner Bindung?

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

Antwort

11

Ninjects sucht Konstrukteuren in the following order:

  1. Konstrukteurs markiert mit [Inject]
  2. Construtors mit den meisten Parameter
  3. Standard contructor

In Ihrem Fall ist Ihr TLPContext Konstruktor nicht mit [Inject] markiert, so dass die 2. Regeln gelten und Ninject versucht, die base class contructor aufzulösen und löst dann die Ausnahme aus.

So können Sie dieses Problem lösen, indem Sie Ihre Konstruktor mit dem InjectAttribute

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

Oder Sie können specify the constructor mit dem ToConstructor Methode Markierung, wenn der TLPContext Registrierung:

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

Das ist seltsam. Ich habe nie .ToConstructor() verwendet, um eine Injektion über Konstruktor zu erhalten. Ich möchte Ninject nicht in mein DataAccess-Projekt einführen. Ich habe deinen Code ausprobiert und es ändert nichts. Aber was ich letztes Mal vergessen habe: NinjectDependencyResolver.cs Quelle nicht gefunden bekomme ich auch ... Irgendwelche Hinweise? Ah, jetzt verstehe ich es. Ich benutze ein WebApi-Projekt anstelle des mvc nur vorher. Scheint Ninject funktioniert hier anders ... – Elisabeth

+0

Was meinst du mit "NinjectDependencyResolver.cs Quelle nicht gefunden"? Wie hast du Ninject in deinem Projekt mit nugget installiert? – nemesv

+0

Ich benutze immer nuget ja. keine manuelle Installation hehe – Elisabeth

2

habe ich ähnliches Problem haben. Ich benutzte Ninject MVC und ich versuchte, die kernel mit dem neuen StandardKernel ctor instanziieren, und es hat nicht funktioniert.

Mein Problem war der Punkt 3, die @Elisa bereits erwähnt: Ensure you have not accidentally created more than one kernel.

Ich löste es durch bootstrapper.Kernel stattdessen verwenden.

Verwandte Themen