4

我試圖建立依賴注入Autofac使用MVC5和EF6項目。類型Rolestore的<IdentityRole>是不能分配給服務IRoleStore <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>>())); 

完整的錯誤消息:

類型「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,文化=中性公鑰= 31bf3856ad364e35]]」。

回答

10

位遲到了,但是這個工作對我來說與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) 
    {    
    }  
} 
相關問題