【问题标题】:How to resolve "Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException: Reading the request body timed out due to data arriving too slowly"?如何解决“Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException:由于数据到达太慢导致读取请求正文超时”?
【发布时间】:2023-03-30 21:38:01
【问题描述】:

在进行 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 个样本中仅 20 到 每个 POST 方法有 40 个样本失败。提供相同的输入,同时 测试。

【问题讨论】:

  • 您的主机模式是什么? Kestrel 或 IIS 或 HTTP.Sys?
  • 您有 IIS 托管解决方案吗?我在 iis 中遇到同样的错误

标签: performance asp.net-core asp.net-core-webapi


【解决方案1】:

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

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

参考:

KestrelServerLimits.MinRequestBodyDataRate Property

【讨论】:

  • 嗨,这个安全吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-05
  • 1970-01-01
  • 1970-01-01
  • 2019-04-01
  • 2019-11-23
  • 2017-07-24
  • 2012-02-18
相关资源
最近更新 更多