因此,我已经挖了很长时间才找到答案,但没有运气。
我究竟做错了什么?
Ninject 使用以下消息引发异常:
Error activating IModelRepository{User}
No matching bindings are available, and the type is not self-bindable.
这是我的代码:
我有一个通用接口:
public interface IModelRepository<T> where T: IModel
{
//interface stuff here
}
具体类是:
public UserRepository : IModelRepository<User>
{
public UserRepository(IDocumentStore documentStore, string databaseName)
{
//constructor code here
}
}
Ninject 模块 Load():
public override void Load()
{
string databaseName = Properties.Settings.Default.DefaultDatabaseName;
Bind<IModelRepository<User>>()
.To<UserRepository>()
.WithConstructorArgument("documentStore", Kernel.Get<IDocumentStore>())
.WithConstructorArgument("databaseName", databaseName);
}
Ninject 实例化(这是发生异常的地方):
Kernel = new Ninject.StandardKernel(new DIModules.ModelRepositoryModule()
,new DIModules.DocumentStoreModule());
这是完整的堆栈跟踪:
at Ninject.KernelBase.Resolve(IRequest request) in c:\Projects\Ninject\ninject\src\Ninject\KernelBase.cs:line 359
at Ninject.ResolutionExtensions.GetResolutionIterator(IResolutionRoot root, Type service, Func`2 constraint, IEnumerable`1 parameters, Boolean isOptional, Boolean isUnique) in c:\Projects\Ninject\ninject\src\Ninject\Syntax\ResolutionExtensions.cs:line 263
at Ninject.ResolutionExtensions.Get[T](IResolutionRoot root, IParameter[] parameters) in c:\Projects\Ninject\ninject\src\Ninject\Syntax\ResolutionExtensions.cs:line 37
at xl.view.DIModules.DataStoreModule.Load() in c:\Users\Michael\Google Drive\Projects\Windows\xl\xl.view\DIModules\DataStoreModule.cs:line 18
at Ninject.Modules.NinjectModule.OnLoad(IKernel kernel) in c:\Projects\Ninject\ninject\src\Ninject\Modules\NinjectModule.cs:line 85
at Ninject.KernelBase.Load(IEnumerable`1 m) in c:\Projects\Ninject\ninject\src\Ninject\KernelBase.cs:line 217
at Ninject.KernelBase..ctor(IComponentContainer components, INinjectSettings settings, INinjectModule[] modules) in c:\Projects\Ninject\ninject\src\Ninject\KernelBase.cs:line 100
at Ninject.KernelBase..ctor(INinjectModule[] modules) in c:\Projects\Ninject\ninject\src\Ninject\KernelBase.cs:line 57
at Ninject.StandardKernel..ctor(INinjectModule[] modules) in c:\Projects\Ninject\ninject\src\Ninject\StandardKernel.cs:line 46
at xl.view.Program.InitializeApplication() in c:\Projects\Windows\xl\xl.view\Program.cs:line 53
at xl.view.Program.Main() in c:\Windows\xl\xl.view\Program.cs:line 28