我正在尝试使用 Autofac 为使用 MVC5 和 EF6 的项目设置依赖项注入。
我很难弄清楚如何正确解耦 EntityFramework.RoleStore 实现。
我只想依赖 Identity.IRoleStore 但我知道对于泛型类,我需要指定具体实现,而不是接口。
这是我尝试过的:
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>>()));
完整的错误消息:
类型“Microsoft.AspNet.Identity.EntityFramework.RoleStore”1[Microsoft.AspNet.Identity.EntityFramework.IdentityRole]' is not assignable to service 'Microsoft.AspNet.Identity.IRoleStore
1[[Microsoft.AspNet.Identity.IRole,Microsoft.AspNet.Identity.Core,版本=1.0.0.0,文化=中性,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)
{
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)