-1

我们已经在干净的架构中实现了一个 asp.net core web api 6.0 项目。 https://github.com/jasontaylordev/CleanArchitecture

所有 API 在开发模式下都可以正常工作。但在生产模式下,API 不起作用。

示例:我通过邮递员 http://localhost:5000/api/deals/public?SiteCode=wpf 发送了一个 get 请求。

我得到了 500 错误。 { "type": "https://tools.ietf.org/html/rfc7231#section-6.6.1", "title": "An error occurred while processing your request.", "status": 500 }

在终端中,输出显示如下

 Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[62]
      User profile is available. Using '/.aspnet/DataProtection-Keys' as key repository; keys will not be encrypted at rest.
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5000
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: https://localhost:5001
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
      Content root path: techne-bo-for-ota/
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
      Request starting HTTP/1.1 GET http://localhost:5000/api/deals/public?SiteCode=wpf - -
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
      Executing endpoint 'WebApi.Controllers.Public.PublicDealController.ListBySiteCode (WebApi)'
info: Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker[3]
      Route matched with {action = "ListBySiteCode", controller = "PublicDeal"}. Executing controller action with signature System.Threading.Tasks.Task`1[Microsoft.AspNetCore.Mvc.ActionResult`1[Application.Deals.Queries.GetDealListBySiteCode.DealListBySiteCodeVm]] ListBySiteCode(Application.Deals.Queries.GetDealListBySiteCode.GetDealListBySiteCodeQuery) on controller WebApi.Controllers.Public.PublicDealController (WebApi).
info: Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor[1]
      Executing ObjectResult, writing value of type 'Microsoft.AspNetCore.Mvc.ProblemDetails'.
info: Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker[2]
      Executed action WebApi.Controllers.Public.PublicDealController.ListBySiteCode (WebApi) in 292.3075ms
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1]
      Executed endpoint 'WebApi.Controllers.Public.PublicDealController.ListBySiteCode (WebApi)'
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
      Request finished HTTP/1.1 GET http://localhost:5000/api/deals/public?SiteCode=wpf - - - 500 132 application/problem+json;+charset=utf-8 487.1733ms

在程序.cs

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers(options =>
                options.Filters.Add<ApiExceptionFilterAttribute>())
                    .AddFluentValidation()
.AddNewtonsoftJson(options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);

var app = builder.Build();

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllers();
});

ApiControllerBase.cs

[ApiController]
[Route("api/[controller]")]
public abstract class ApiControllerBase : ControllerBase
{
    private ISender _mediator = null!;

    protected ISender Mediator => _mediator ??= HttpContext.RequestServices.GetRequiredService<ISender>();
}

PublicDealController.cs

[Route("api/deals/public")]
[AllowAnonymous]
public class PublicDealController : ApiControllerBase
{
    [HttpGet]
    [ProducesResponseType(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status500InternalServerError)]
    public async Task<ActionResult<DealListBySiteCodeVm>> ListBySiteCode([FromQuery] GetDealListBySiteCodeQuery query)
    {
        var result = await Mediator.Send(query);

        return Ok(result);
    }
}

我犯了什么错误?为什么 API 不能在生产模式下工作。有人遇到过这个问题吗?请帮忙找出错误。我不知道为什么 api 不能仅在生产模式下工作。

4

0 回答 0