RoleStore< IdentityRole>类型不可分配给服务IRoleStore< IRole> [英] The type RoleStore<IdentityRole> is not assignable to service IRoleStore<IRole>

查看:287
本文介绍了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>>()));

完整的错误消息:


类型Microsoft.AspNet.Identity.EntityFramework.RoleStore 1 [Microsoft.AspNet.Identity.EntityFramework.IdentityRole]不能分配给服务Microsoft.AspNet.Identity.IRoleStore 1 [[Microsoft.AspNet.Identity.IRole,Microsoft.AspNet.Identity.Core,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35]]'。



解决方案

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



< 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:

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]]'.

解决方案

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;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆