我正在尝试使用MVC5和EF6为项目设置依赖注入Autofac. 我很难弄清楚如何正确地解耦EntityFramework.RoleStore EntityFramework.IdentityRole实现. 我想只依赖于Identity.IRoleStore Identity.IRole但我知道对于泛型
我很难弄清楚如何正确地解耦EntityFramework.RoleStore< EntityFramework.IdentityRole>实现.
我想只依赖于Identity.IRoleStore< Identity.IRole>但我知道对于泛型类,我需要指定具体的实现,而不是接口.
这是我试过的:
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来说很有用:The type ‘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, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]’.
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) { } }