加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

依赖注入 – 类型RoleStore不能分配给服务IRoleStore

发布时间:2020-12-13 20:45:26 所属栏目:百科 来源:网络整理
导读:我正在尝试使用MVC5和EF6为项目设置依赖注入Autofac. 我很难弄清楚如何正确地解耦EntityFramework.RoleStore EntityFramework.IdentityRole实现. 我想只依赖于Identity.IRoleStore Identity.IRole但我知道对于泛型类,我需要指定具体的实现,而不是接口. 这是
我正在尝试使用MVC5和EF6为项目设置依赖注入Autofac.

我很难弄清楚如何正确地解耦EntityFramework.RoleStore< EntityFramework.IdentityRole>实现.
我想只依赖于Identity.IRoleStore< Identity.IRole>但我知道对于泛型类,我需要指定具体的实现,而不是接口.

这是我试过的:

builder.RegisterType<IdentityRole>().As<IRole>();
        builder.RegisterType<RoleManager<IRole>>();
        builder.RegisterType<RoleStore<IdentityRole>>().As<IRoleStore<IRole>>();
        builder.Register(c => new RoleManager<IRole>(c.Resolve<IRoleStore<IRole>>()));

完整的错误消息:

The type ‘Microsoft.AspNet.Identity.EntityFramework.RoleStore1[Microsoft.AspNet.Identity.EntityFramework.IdentityRole]' is not assignable to service 'Microsoft.AspNet.Identity.IRoleStore1[[Microsoft.AspNet.Identity.IRole,Microsoft.AspNet.Identity.Core,Version=1.0.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35]]’.

派对迟到了,但这对Autofac来说很有用:
builder.RegisterType<RoleStore<IdentityRole>>().As<IRoleStore<IdentityRole,string>>();

我的完整模块供参考:

builder.RegisterType<UserStore<ApplicationUser>>().As<IUserStore<ApplicationUser>>();
builder.RegisterType<RoleStore<IdentityRole>>().As<IRoleStore<IdentityRole,string>>();
builder.RegisterType<ApplicationUserManager>();
builder.RegisterType<ApplicationRoleManager>();

我正在使用UserManager和RoleManager的包装器

public class ApplicationUserManager : UserManager<ApplicationUser>
{
    public ApplicationUserManager(IUserStore<ApplicationUser> store)
        : base(store)
    {
    }
}

public class ApplicationRoleManager : RoleManager<IdentityRole>
{
    public ApplicationRoleManager(IRoleStore<IdentityRole,string> roleStore)
        : base(roleStore)
    {            
    }       
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读