【问题标题】:Dependency injection RoleStore with ninject依赖注入 RoleStore 与 ninject
【发布时间】:2023-12-13 22:55:01
【问题描述】:

嘿,我正在尝试将 IRoleStore 绑定到 Rolestore,但我不断收到错误消息。我为应用程序用户使用相同的代码,效果很好,但作为示例,我也会在这里展示一个:

kernel.Bind<IRoleStore<ApplicationRole>>().To<RoleStore<ApplicationRole>>()
    .WithConstructorArgument("context", 
        context => kernel.Get<InBuildingNavigatorDbContext>());
kernel.Bind<RoleManager<ApplicationRole>>().ToSelf().InRequestScope();

kernel.Bind<IUserStore<ApplicationUser>>().To<UserStore<ApplicationUser>>()
    .WithConstructorArgument("context", 
        context => kernel.Get<InBuildingNavigatorDbContext>());
kernel.Bind<UserManager<ApplicationUser>>().ToSelf().InRequestScope();

我得到了错误:

类型“Microsoft.AspNet.Identity.EntityFramework.RoleStore”不能用作泛型类型或方法“IBindingToSyntax>.To”中的类型参数“TImplementation” ()'。没有从 'Microsoft.AspNet.Identity.EntityFramework.RoleStore' 到 'Microsoft.AspNet.Identity.IRoleStore' 的隐式引用转换。

有什么想法吗?

【问题讨论】:

    标签: c# entity-framework ninject owin identity


    【解决方案1】:

    盲拍,但你可以试试:

    kernel.Bind<IRoleStore<ApplicationRole, string>>().To<RoleStore<ApplicationRole>>()
        .WithConstructorArgument("context", 
            context => kernel.Get<InBuildingNavigatorDbContext>());
    

    根据RoleStore documentation RoleStore 实现IRoleStore&lt;TRole, string&gt;,而不是IRoleStore&lt;TRole&gt;

    【讨论】:

    • 我试过这个,它确实修复了错误,但我重新做了整个角色的事情,所以它不再相关了。但无论如何,谢谢,它确实修复了它! :)