本文介绍了RoleStore< IdentityRole>类型不可分配给服务IRoleStore< 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>>()。作为< IRoleStore< IRole>>();
builder.Register(c => new RoleManager< IRole>(c.Resolve< IRoleStore< IRole>>()));

完整的错误消息:


解决方案

晚点参加派对,但这对于我自动填充:



< pre> builder.RegisterType< RoleStore< IdentityRole>>()。作为< IRoleStore< IdentityRole,string>>()

我的完整模块供参考:

  builder.RegisterType< UserStore< ApplicationUser>>()为< IUserStore< ApplicationUser>>(); 
builder.RegisterType< RoleStore< IdentityRole>>()。作为< 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)
{
}
}


I'm trying to set up dependency injection with Autofac for project using MVC5 and EF6.

I'm having a hard time figuring out how to decouple correctly the EntityFramework.RoleStore<EntityFramework.IdentityRole> implementation.
I would like have dependency only on Identity.IRoleStore<Identity.IRole> but I'm aware that for generic classes I need to specify the concrete implementation, not the interface.

This is what I tried:

        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 full error message:

解决方案

Bit late to the party but this worked for me with Autofac:

builder.RegisterType<RoleStore<IdentityRole>>().As<IRoleStore<IdentityRole, string>>();

My full module for reference:

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

I'm using wrappers for the UserManager and 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)
    {            
    }       
}

这篇关于RoleStore&lt; IdentityRole&gt;类型不可分配给服务IRoleStore&lt; IRole&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 16:05