[英]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
,它是IQueryHandler
和IQueryHandlerAsync
。 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.