繁体   English   中英

带有依赖注入的构造函数在react asp.net核心应用程序中导致HTTP 500错误

[英]Constructor with Dependency Injection is causing http 500 error in react asp.net core application

以下控制器处理使用操作,例如注册。 但是,当我使用axios在控制器中调用Register方法时,收到以下错误。

加载资源失败:服务器响应状态为500()

但是,如果我在构造函数中注释了代码,则调用成功完成。 我不明白为什么构造函数会创建此问题。 有什么建议么?

public class ApplicationUserController : ControllerBase
{
    private IUserService _userService;
    private IMapper _mapper;
    private readonly AppSettings _appSettings;

    public ApplicationUserController(
        IUserService userService,
        IMapper mapper,
        IOptions<AppSettings> appSettings
    )
    {
        _userService = userService;
        _mapper = mapper;
        _appSettings = appSettings.Value;
    }

    [AllowAnonymous]
    [HttpPost]
    [Route("api/ApplicationUser/Register")]
    public IActionResult Register([FromBody]ApplicationUserDto userDto)
    { 
         //implementation details
    }
}

这是IUSerServices接口:

public interface IUserService
{
    ApplicationUser Authenticate(string username, string password);

}

public class UserService : IUserService
{
    private myDbContext _context;

    public UserService(myDbContext context)
    {
        _context = context;
    }

    public ApplicationUser Authenticate(string username, string password)
    {
        //implementation details
    }
}

DI在Startup.cs注册

services.AddScoped<IUserService, UserService>();
services.AddScoped<myDbContext>();

IMapper是AutoMapper库中的接口。

您需要添加软件包AutoMapper.Extensions.Microsoft.DependencyInjection并调用

services.AddAutoMapper();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM