Sometimes, handling request in controller
[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");
}
throw exception:
ail: Microsoft.AspNetCore.Server.Kestrel[13]
Connection id "0HMU4QP2AD7CS", Request id "0HMU4QP2AD7CS:00000001": An unhandled exception was thrown by the application.
Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException: Unexpected end of request content.
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1ContentLengthMessageBody.ReadAsyncInternal(CancellationToken cancellationToken)
at System.Runtime.CompilerServices.PoolingAsyncValueTaskMethodBuilder1.StateMachineBox1.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpRequestStream.ReadAsyncInternal(Memory1 destination, CancellationToken cancellationToken) at System.Runtime.CompilerServices.PoolingAsyncValueTaskMethodBuilder1.StateMachineBox1.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](IHttpApplication1 application)
Users quickly press buttons on the front, up to 1 time per second. Every time you click from the frontend, an ajax request is made.
it seems a dotnet bug, see https://github.com/dotnet/aspnetcore/issues/23949, you can use a middleware to catch the Exception, https://github.com/dotnet/aspnetcore/issues/23949#issuecomment-950471048