2013-02-12 8 views
9

Sto usando Ninjec, Ninject.Web.MVC e Ninject.Web.CommonNo binding di corrispondenza sono disponibili, e il tipo non è auto-associabile in Ninject

Quando comincio la mia applicazione MVC ottengo questo errore vincolante :

Cosa devo fare nel mio binding?

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

risposta

11

Ninjects cerca i costruttori in the following order:

  1. Costruttori contrassegnati con [Inject]
  2. Construtors con il maggior numero di parametri
  3. contructor predefinito

Nel tuo caso il tuo costruttore TLPContext non è contrassegnato con [Inject] quindi le 2. regole si applicano e Ninject proverà a risolvere lo base class contructor e quindi lancia l'eccezione.

Così si può risolvere questo contrassegnando il vostro costruttore con il InjectAttribute

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

Oppure si può specify the constructor con il metodo ToConstructor al momento della registrazione la tua TLPContext:

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

Questo è dispari. Non ho mai usato .ToConstructor() per ottenere l'iniezione tramite il costruttore. Non voglio introdurre Ninject nel mio progetto DataAccess. Ho provato il tuo codice e non cambia nulla. Ma cosa ho dimenticato l'ultima volta: la fonte NinjectDependencyResolver.cs non è stata trovata Ho anche ... Qualche idea? Ah ora ho capito. Io uso solo un progetto WebApi al posto del mvc. Sembra che Ninject funzioni qui in modo diverso ... – Elisabeth

+0

Che cosa intendi per "origine NinjectDependencyResolver.cs non trovata"? Come hai installato Ninject nel tuo progetto con nuget? – nemesv

+0

Io uso sempre nuget si. nessuna installazione manuale hehe – Elisabeth

2

ho usato per avere problema simile. Stavo usando Ninject MVC e ho provato a istanziare il kernel usando il nuovo StandardKernel ctor, e non funzionava.

Il mio problema è stato il punto 3 che @Elisa accennato in precedenza: Ensure you have not accidentally created more than one kernel.

ho risolto utilizzando bootstrapper.Kernel invece.

Problemi correlati