2

因此,我已经挖了很长时间才找到答案,但没有运气。

我究竟做错了什么?

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
4

2 回答 2

1

尝试更改模块的顺序,似乎顺序很重要,因为在绑定它们之前IModelRepository<User>不知道:IModelUser

Kernel = new Ninject.StandardKernel(
    new DIModules.DocumentStoreModule(),
    new DIModules.ModelRepositoryModule());

这对我很有效,这里是完整的示例: http: //pastebin.com/2TjBqAwc

于 2012-08-19T05:48:13.373 回答
1
 .WithConstructorArgument("documentStore", Kernel.Get<IDocumentStore>())

您可能希望将其更改为. 您调用它的方式是在模块期间创建对象- 这不应该是 casse - Moduel方法应该只是填充。ctx=> Kernel.Get<IDocumentStore>()Load()Load()Bind()

此外,手头没有开发环境,但很确定应该有一种方法让默认配置负责将该 ctor 参数绑定到任何 DI 将解决的问题。

(如果以上都没有意义,你肯定需要提供比你更完整的堆栈跟踪)

于 2012-08-19T20:19:01.047 回答