【问题标题】:Can not create objects of Identity classes (UserManager and RoleManager)无法创建身份类的对象(UserManager 和 RoleManager)
【发布时间】: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;
        }
    }

更新

当我尝试创建自己的 RoleManagerUserManager 类时,我得到了同样的错误。

我的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 注册为实现。
  • IUnitOfWorkIdentityUnitOfWork 已经在我的 API 层的Startup.cs 中注册,但这对我没有帮助。

标签: c# asp.net-core asp.net-identity identity data-access-layer


【解决方案1】:

您已将身份服务添加到 IoC 容器。所以你可以像这样在 UnitOfWork 的构造函数中使用依赖注入:

public class IdentityUnitOfWork : IUnitOfWork
{
    private UserManager<IdentityUser> _userManager;
    private RoleManager<IdentityRole> _roleManager;
    private ApplicationContext _context;

    public IdentityUnitOfWork(ApplicationContext context, 
        UserManager<IdentityUser> userManager,
        RoleManager<IdentityRole> roleManager)
    {
        _userManager = userManager;
        _roleManager = roleManager;
        _context = context;
    }
}

【讨论】:

  • 这解决了我的问题,但我不确定在构造函数中传递 UserManager 和 RoleManager 是否正确。
  • 看来你对依赖注入并不熟悉。当您将身份作为服务添加到应用程序时,UserManager 和 RoleManager 的实例在 IoC 容器中可用。因此,您可以将它们注入 uow。它不是传递参数,而是依赖注入。你如何初始化uow??
  • 在业务逻辑层我无法获得UserManagerRoleManager 的实例,因为它们没有在uow 中初始化
  • 应用程序启动后,您可以在代码中随处获取实例。我猜你正在尝试使用 new 关键字获取 instancec。
  • 当我尝试获取 UserManagerRoleManager 时出现此错误:'IUnitOfWork' does not contain a definition for 'RoleManager' and no accessible extension method 'RoleManager' accepting a first argument of type 'IUnitOfWork' could be found (are you missing a using directive or an assembly reference?) 我可以从 UnitOfWork 获取所有其他属性,因为它们是在 get 方法中初始化的,如果我删除初始化,我不能像UserManagerRoleManager 那样得到它们
猜你喜欢
  • 2021-08-05
  • 1970-01-01
  • 2019-07-27
  • 2021-11-13
  • 1970-01-01
  • 2016-07-31
  • 2017-03-07
  • 1970-01-01
  • 2022-10-06
相关资源
最近更新 更多