首页 > 解决方案 > 如何解决“Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException:由于数据到达太慢导致读取请求正文超时”?

问题描述

在进行 API 性能测试时 [例如,线程数/用户数:50,循环计数:10],由于以下异常,使用 POST 方法的 5% 到 8% 样本失败:

**Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException: Reading the request body timed out due to data arriving too slowly. See MinRequestBodyDataRate.**
   at Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException.Throw(RequestRejectionReason reason)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1MessageBody.PumpAsync()
   at System.IO.Pipelines.PipeCompletion.ThrowLatchedException()
   at System.IO.Pipelines.Pipe.GetReadResult(ReadResult& result)
   at System.IO.Pipelines.Pipe.GetReadAsyncResult()
   at System.IO.Pipelines.Pipe.DefaultPipeReader.GetResult(Int16 token)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.MessageBody.ReadAsync(Memory`1 buffer, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpRequestStream.ReadAsyncInternal(Memory`1 buffer, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.WebUtilities.FileBufferingReadStream.ReadAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.WebUtilities.StreamHelperExtensions.DrainAsync(Stream stream, ArrayPool`1 bytePool, Nullable`1 limit, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Mvc.Formatters.JsonInputFormatter.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)

你能建议我如何解决这个问题吗?

注意:我在 POST 方法中正确实现了 async/await。这仅发生在 POST 方法中,在 500 个样本中,每个 POST 方法只有 20 到 40 个样本失败。在测试时提供相同的输入。

标签: performanceasp.net-coreasp.net-core-webapi

解决方案


您可以将KestrelServerLimits.MinRequestBodyDataRate属性设置为 null,Program.cs如下所示:

.UseKestrel(opt =>
{
    opt.Limits.MinRequestBodyDataRate = null;
});

参考:

KestrelServerLimits.MinRequestBodyDataRate 属性


推荐阅读