【发布时间】:2020-08-10 09:53:06
【问题描述】:
我正在开发一个 3 层架构应用程序,因此我将 UserManager 和 RoleManager 添加到数据访问层的 UnitOfWork。 但是当我尝试创建 UserManager 和 RoleManager 类的对象时,我得到了这个错误:
There is no argument given that corresponds to the required formal parameter 'optionsAccessor'
of 'UserManager<IdentityUser>.UserManager(IUserStore<IdentityUser>, IOptions<IdentityOptions>,
IPasswordHasher<IdentityUser>, IEnumerable<IUserValidator<IdentityUser>>,
IEnumerable<IPasswordValidator<IdentityUser>>, ILookupNormalizer, IdentityErrorDescriber,
IServiceProvider, ILogger<UserManager<IdentityUser>>)'
There is no argument given that corresponds to the required formal parameter 'roleValidators'
of 'RoleManager<IdentityRole>.RoleManager(IRoleStore<IdentityRole>, IEnumerable<IRoleValidator<IdentityRole>>,
ILookupNormalizer, IdentityErrorDescriber, ILogger<RoleManager<IdentityRole>>)'
我的 UnitOfWork 课程的一部分
public class IdentityUnitOfWork : IUnitOfWork
{
private UserManager<IdentityUser> _userManager;
private RoleManager<IdentityRole> _roleManager;
private ApplicationContext _context;
public IdentityUnitOfWork(ApplicationContext context)
{
_userManager = new UserManager<IdentityUser>(context);// error 1
_roleManager = new RoleManager<IdentityRole>(context);// error 2
_context = context;
}
}
更新
当我尝试创建自己的 RoleManager 和 UserManager 类时,我得到了同样的错误。
我的ApplicationRole 班级
public class ApplicationRole : IdentityRole
{
}
我的ApplicationRoleManager 班级
public class ApplicationRoleManager : RoleManager<ApplicationRole>
{
public ApplicationRoleManager(RoleStore<ApplicationRole> store)
: base(store)// error in a here (in a base)
{
}
}
【问题讨论】:
-
你可以简单地用类注入你的构造函数。确保将
IUnitOfWork注册为服务,并将IdentityUnitOfWork注册为实现。 -
IUnitOfWork和IdentityUnitOfWork已经在我的 API 层的Startup.cs中注册,但这对我没有帮助。
标签: c# asp.net-core asp.net-identity identity data-access-layer