Skip to main content
 首页 » 编程设计

c#之Ninject.Extensions.Factory : cannot find binding

2024年02月20日12kenshinobiy

我的 Ninject.MVC RegisterServices 中有以下行:

kernel.Load(new NinjectBllModule()); 
kernel.Bind<IMembershipLogicFactory>().ToFactory(); 

在我的模块中:

 Bind<IUserLogic<LoginUser>>().To<UserLogic>(); 
 Bind<IRoleLogic<SimpleRole, LoginUser>>().To<RoleLogic>(); 

和我的 IMembershipLogicFactory:

public interface IMembershipLogicFactory 
{ 
    IUserLogic<TUser> GetUserLogic<TUser>() where TUser : UserBase; 
    IRoleLogic<TRole, TUser> GetRoleLogic<TRole, TUser>() where TRole : RoleBase<TUser> where TUser : UserBase<TRole>; 
} 

然而,当我注入(inject) IMembershipLogicFactory 并在其上调用 GetUserLogic() 时,出现以下错误:

Error activating IUserLogic{LoginUser} 
No matching bindings are available, and the type is not self-bindable. 
Activation path: 
 1) Request for IUserLogic{LoginUser} 

而且我似乎找不到我做错了什么。

请您参考如下方法:

扩展使用以 Get 开头的方法解析命名绑定(bind)的约定。 GetUserLogic 因此会被翻译成

kernel.Get<IUserLogic<LoginUser>>("UserLogic"); 

没有定义绑定(bind)。只需使用另一个方法名称,例如 CreateUserLogic