C# 没有匹配的绑定可用,并且类型在 Ninject 中不可自绑定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14840515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
No matching bindings are available, and the type is not self-bindable in Ninject
提问by Elisabeth
I am using Ninjec, Ninject.Web.MVC and Ninject.Web.Common
我正在使用 Ninjec、Ninject.Web.MVC 和 Ninject.Web.Common
When I start my mvc application I get this binding error:
当我启动我的 mvc 应用程序时,我收到此绑定错误:
What do I wrong in my 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.
激活 DbConnection 时出错
没有匹配的绑定可用,并且类型不可自绑定。
激活路径:
4) 将依赖项 DbConnection 注入 DbContext 类型构造函数的参数 existingConnection
3) 将依赖项 DbContext 注入到 GenericRepository{User} 类型的构造函数的参数 dbContext 中
2) 将依赖 IGenericRepository{User} 注入 HomeController 类型的构造函数的参数 repo
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; }
}
采纳答案by nemesv
Ninjects looks for constructors in the following order:
Ninjects按以下顺序查找构造函数:
- Constructors marked with
[Inject]
- Construtors with the most parameter
- Default contructor
- 带有标记的构造函数
[Inject]
- 参数最多的构造函数
- 默认构造函数
In your case your TLPContext
constructor is not marked with [Inject]
so the 2. rules applies and Ninject will try to resolve the base class contructorand then throws the exception.
在您的情况下,您的TLPContext
构造函数未标记[Inject]
为 2. 规则适用,Ninject 将尝试解析基类构造函数,然后抛出异常。
So you can solve this by marking your constructor with the InjectAttribute
所以你可以通过标记你的构造函数来解决这个问题 InjectAttribute
[Inject]
public TLPContext()
: base("DefaultConnection")
{
this.Configuration.LazyLoadingEnabled = false;
}
Or you can specify the constructorwith the ToConstructor
method when registering your TLPContext
:
或者,您可以在注册您的方法时使用该方法指定构造函数:ToConstructor
TLPContext
kernel.Bind<TLPContext>().ToConstructor(_ => new TLPContext());
回答by Jaider
I used to have similar problem. I was using Ninject MVC
and I tried to instantiate the kernel
using the new StandardKernel
ctor, and it did't work.
我曾经有过类似的问题。我正在使用,Ninject MVC
并尝试kernel
使用新的StandardKernel
ctor来实例化,但它不起作用。
My problem was the point 3 that @Elisa mentioned earlier: Ensure you have not accidentally created more than one kernel.
我的问题是@Elisa 之前提到的第 3 点: Ensure you have not accidentally created more than one kernel.
I solved it by using bootstrapper.Kernel
instead.
我通过使用bootstrapper.Kernel
来解决它。