问题描述
我正在尝试使用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< IdentityRole>类型不可分配给服务IRoleStore< IRole>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!