简体   繁体   English

Ninject:同一类有两次以上的匹配绑定可用

[英]Ninject: More than one matching bindings are available same class twice

Given this class: 给定此类:

public class UserQueryHandler : 
    IQueryHandler<UserCredentialsByUsernameQuery, UserJWTIdentity>,
    IQueryHandlerAsync<UserRoleByUserNameQuery, UserRole>
{

//..implementation } //..implementation}

public interface IQueryHandler<TParameter, TResult>
{
    TResult Retrieve(TParameter query);
}
public interface IQueryHandlerAsync<TParameter, TResult>
{
    Task<TResult> RetrieveAsync(TParameter query);
}

And the following Ninject configuration: 以及以下Ninject配置:

kernel.Bind(x => x
       .FromThisAssembly()
       .SelectAllClasses().InheritedFrom(typeof(IQueryHandler<,>))
       .BindAllInterfaces());

            kernel.Bind(x => x
             .FromThisAssembly()
             .SelectAllClasses().InheritedFrom(typeof(IQueryHandlerAsync<,>))
             .BindAllInterfaces());

I'm getting the following error: 我收到以下错误:

More than one matching bindings are available. 可以使用多个匹配的绑定。

Matching bindings: 匹配绑定:

1) binding from IQueryHandlerAsync{UserRoleByUserNameQuery, UserRole} to UserQueryHandler 1)从IQueryHandlerAsync {UserRoleByUserNameQuery,UserRole}绑定到UserQueryHandler

2) binding from IQueryHandlerAsync{UserRoleByUserNameQuery, UserRole} to UserQueryHandler 2)从IQueryHandlerAsync {UserRoleByUserNameQuery,UserRole}绑定到UserQueryHandler

Activation path: 激活路径:

1) Request for IQueryHandlerAsync{UserRoleByUserNameQuery, UserRole} 1)请求IQueryHandlerAsync {UserRoleByUserNameQuery,UserRole}

When trying to get an instance using this: 当尝试使用此实例时:

var handler = _kernel.Get<IQueryHandlerAsync<UserRoleByUserNameQuery, UserRole>>();

The suspicious thing is that I don't get the error trying to instantiate the other interface implementation: 可疑的是,我没有在尝试实例化其他接口实现时收到错误:

var handler = _kernel.Get<IQueryHandler<UserCredentialsByUsernameQuery, UserJWTIdentity>>();

And the error disapears if I create a separated class that implements only that interface: 如果我创建一个仅实现该接口的单独类,该错误就会消失:

public class UserQueryHandler2 : 
    IQueryHandlerAsync<UserRoleByUserNameQuery, UserRole>
{
//..implementation works fine!! }

From what I understand, the class is being binded twice, but I don't understand why is this happening (probably a bug?) 据我了解,该类被绑定了两次,但是我不明白为什么会发生这种情况(可能是一个错误?)

BindAllInterfaces is binding all the interfaces the type inherits from, which in the case of UserQueryHandler is both IQueryHandler and IQueryHandlerAsync . BindAllInterfaces绑定该类型继承的所有接口,就UserQueryHandler ,它是IQueryHandlerIQueryHandlerAsync So your type is being bound to IQueryHandlerAsync in the first portion of your Ninject configuration when it's scanning for all types that inherit from IQueryHandler . 所以你的类型被绑定到IQueryHandlerAsync在Ninject配置的第一部分时,它的扫描,从继承的所有类型IQueryHandler

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ninject - 使用单个接口的多个类(可以使用多个匹配的绑定) - Ninject - Multiple Classes Using Single Interface (more than one matching bindings are available) Ninject:没有匹配的绑定可用,并且类型不可自绑定 - Ninject: No matching bindings are available, and the type is not self-bindable Ninject WithConstructorArgument:没有匹配的绑定可用,并且该类型不可自绑定 - Ninject WithConstructorArgument : No matching bindings are available, and the type is not self-bindable Ninject Activation Error:没有匹配的绑定可用,并且该类型不可自绑定 - Ninject Activation Error: No matching bindings are available, and the type is not self-bindable 没有匹配的绑定可用,并且该类型在Ninject中不可自绑定 - No matching bindings are available, and the type is not self-bindable in Ninject Ninject / NHibernate配置多个数据库; 绑定到提供程序Ninject.Web.MVC3和.NET 4.5时,可以使用多个绑定 - Ninject/NHibernate configuring multiple databases; more than one binding available when binding to provider Ninject.Web.MVC3 and .NET 4.5 是否可以设置Ninject绑定来创建同一类的多个实例? - Is it possible to setup Ninject bindings to create multiple instances of same class? 具有ninject和通用接口的自动映射器不起作用(错误激活/没有匹配的绑定可用) - Automapper with ninject and generic interface won't work (error activating/no matching bindings available) Ninject 2.0 - 绑定到多次使用相同接口的对象? - Ninject 2.0 - binding to a object that uses the same interface more than once? Ninject:如何绑定具有多个类型参数的开放泛型? - Ninject: How to bind an open generic with more than one type argument?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM