Asp.net 7 核心。 Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException:请求内容意外结束

问题描述 投票:0回答:1

有时,在控制器中处理请求

[ApiController]
[Route("api")]
public class FrontWcController : ControllerBase
{
[HttpPost("change")]
    public IActionResult GetFromFrontSendToErp(ParametrsForButton requestData)
    {
        string wcGuid = requestData.WcGuid;
        string operation = requestData.Operation;
        string skusSerial = requestData.SkusSerial;
        string boxSerial = requestData.BoxSerial;
        int actionNumber = requestData.ActionNumber;
        int operationNumber = requestData.OperationNumber;
        int checkBox = requestData.CheckBox;

        string actionHumanString = "Undefined";

        switch (actionNumber)
        {
            case 1:
                actionHumanString = "Started";
                break;
            case 2:
                actionHumanString = "Pause";
                break;
            case 3:
                actionHumanString = "Finished";
                break;
            case 4:
                actionHumanString = "Exeption";
                break;
            case 5:
                actionHumanString = "Started";
                break;
            default:
                Console.WriteLine("Cant change status. Set to default");
                break;
        }

        DateTime dateTimeOfOperation = DateTime.UtcNow;
        
        MainIncomingChangesSaverList.IncomingChangesSaversList.Enqueue(new IncomingChangesSaver()
        {
            WcGuid = wcGuid,
            Operation = operation,
            SkusSerial = skusSerial,
            BoxSerial = boxSerial,
            ActionNumber = actionNumber,
            OperationNumber = operationNumber,
            CheckBox = checkBox,
            ActionHumanString = actionHumanString,
            DateTimeOfChange = dateTimeOfOperation
        });

        using (ApplicationContext db = new ApplicationContext())
        {
            if (actionNumber == 2 || actionNumber == 5)
            {
                Pause pause = new Pause(){wcGuid = wcGuid, Operation = operation, BoxSerial = boxSerial, SkusSerial = skusSerial, ActionNumber = actionNumber, DateTime = dateTimeOfOperation};
                db.Pauses.Add(pause);
            }
            Change change = new Change() { wcGuid = wcGuid, Operation = operation, BoxSerial = boxSerial, SkusSerial = skusSerial, ActionNumber = actionNumber, DateTime = dateTimeOfOperation };
            db.Changes.Add(change);
            db.SaveChanges();
        }

        // return StatusCode(200);
        return Ok("Request successfuly completed");
    }

抛出异常:

ail:Microsoft.AspNetCore.Server.Kestrel[13] 连接 ID“0HMU4QP2AD7CS”,请求 ID“0HMU4QP2AD7CS:00000001”:应用程序引发了未处理的异常。 Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException:请求内容意外结束。 在 Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1ContentLengthMessageBody.ReadAsyncInternal(CancellationToken CancellationToken) 在System.Runtime.CompilerServices.PoolingAsyncValueTaskMethodBuilder

1.StateMachineBox
1.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16令牌) 在 Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpRequestStream.ReadAsyncInternal(内存
1 destination, CancellationToken cancellationToken) at System.Runtime.CompilerServices.PoolingAsyncValueTaskMethodBuilder
1.StateMachineBox
1.System.Threading.Tasks.Sources.IValueTaskSource<TResult>.GetResult(Int16 token) at System.Text.Json.Serialization.ReadBufferState.ReadFromStreamAsync(Stream utf8Json, CancellationToken cancellationToken, Boolean fillBuffer) at System.Text.Json.JsonSerializer.ReadFromStreamAsync[TValue](Stream utf8Json, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken) at Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonInputFormatter.ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding) at Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonInputFormatter.ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding) at Microsoft.AspNetCore.Mvc.ModelBinding.Binders.BodyModelBinder.BindModelAsync(ModelBindingContext bindingContext) at Microsoft.AspNetCore.Mvc.ModelBinding.ParameterBinder.BindModelAsync(ActionContext actionContext, IModelBinder modelBinder, IValueProvider valueProvider, ParameterDescriptor parameter, ModelMetadata metadata, Object value, Object container) at Microsoft.AspNetCore.Mvc.Controllers.ControllerBinderDelegateProvider.<>c__DisplayClass0_0.<<CreateBinderDelegate>g__Bind|0>d.MoveNext() --- End of stack trace from previous location --- at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope) at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger) at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication
1 个应用程序)

用户快速按下前面的按钮,每秒最多 1 次。每次您从前端单击时,都会发出 ajax 请求。

c# .net asp.net-core nginx kestrel
1个回答
0
投票

这似乎是一个dotnet错误,请参阅https://github.com/dotnet/aspnetcore/issues/23949,您可以使用中间件来捕获异常,https://github.com/dotnet/aspnetcore/issues /23949#issuecomment-950471048

© www.soinside.com 2019 - 2024. All rights reserved.