簡體   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