我正在使用Ninjec,Ninject.Web.MVC和Ninject.Web.Common
启动我的MVC应用程序时,出现此绑定错误:
我在装订中有什么错?
激活DbConnection时出错
没有匹配的绑定可用,并且类型不能自绑定。
激活路径:
4)将依赖项DbConnection注入参数类型为DbContext的构造函数的existingConnection
3)将依赖项DbContext注入参数的dbContext中类型GenericRepository {User}
的构造函数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寻找构造函数in the following order:
[Inject]
的构造函数在您的情况下,TLPContext
构造函数未标记为[Inject]
,因此适用2.规则,Ninject将尝试解析base class contructor,然后引发异常。
因此您可以通过用InjectAttribute
标记构造函数来解决此问题>
[Inject] public TLPContext() : base("DefaultConnection") { this.Configuration.LazyLoadingEnabled = false; }
或者您可以在注册specify the constructor时使用
ToConstructor
方法使用TLPContext
:
kernel.Bind<TLPContext>().ToConstructor(_ => new TLPContext());
我曾经有过类似的问题。我正在使用Ninject MVC
,并尝试使用新的kernel
ctor实例化StandardKernel
,但它不起作用。