【问题标题】:An unhandled exception occurred while processing the constructor in ASP.NET Core MVC在 ASP.NET Core MVC 中处理构造函数时发生未处理的异常
【发布时间】:2021-12-06 18:49:26
【问题描述】:

我使用以下代码对注册/登录控制器进行依赖注入。问题是我不能使用我之前创建的存储库模式。

private readonly UserManager<AppUser> _userManager;
        private readonly SignInManager<AppUser> _signInManager;
        private readonly IUserRepository _userRepository;

        public HomeController(UserManager<AppUser> userManager, SignInManager<AppUser> signInManager, IUserRepository userRepository)
        {
            _userManager = userManager;
            _signInManager = signInManager;
            _userRepository = userRepository;
        }

当我运行代码时,出现以下错误:

InvalidOperationException:无法解析服务类型 'CustomizedIdentity.Repositories.IUserRepository' 尝试 激活“CustomizedIdentity.Controllers.HomeController”。

我需要使用存储库从用户表中获取信息。 请指导我。

【问题讨论】:

    标签: asp.net-core repository


    【解决方案1】:

    我找到了自己的答案。我需要使用以下代码来创建服务:

    builder.Services.AddScoped<IUserRepository, UserRepository>();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-22
      • 1970-01-01
      • 1970-01-01
      • 2022-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多