问题描述
我正在尝试使用Autofac为使用MVC5和EF6的项目设置依赖项注入.
I'm trying to set up dependency injection with Autofac for project using MVC5 and EF6.
我很难弄清楚如何正确解耦EntityFramework.RoleStore< EntityFramework.IdentityRole>.实施.
我只想依赖Identity.IRoleStore< Identity.IRole>但是我知道对于泛型类,我需要指定具体的实现,而不是接口.
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.
这是我尝试过的:
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>>()));
完整的错误消息:
推荐答案
参加聚会有点晚,但是这对Autofac来说对我有用:
Bit late to the party but this worked for me with 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使用包装器
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& gt;不能分配给服务IRoleStore< IRole& gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!