我使用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; }
}
這很奇怪。我從來沒有用過.ToConstructor()來通過構造函數來獲取注入。我不想將Ninject引入到我的DataAccess項目中。我試過你的代碼,它並沒有改變。但是,我上次忘了:NinjectDependencyResolver.cs源找不到我也得到...任何線索?啊,現在我明白了。我之前只使用WebApi項目而不是mvc。似乎Ninject在這裏以不同的方式工作... – Elisabeth 2013-02-12 20:40:22
「NinjectDependencyResolver.cs源找不到」是什麼意思?你是如何在nuget的項目中安裝Ninject的? – nemesv 2013-02-12 20:43:13
我總是使用nuget yes。沒有手動安裝hehe – Elisabeth 2013-02-12 20:44:12