Request Body Too Large - Kestrel -Fails at context.body.copytoasync(stream)

Siva Karthik Narisetty 1 Reputation point
2021-08-08T03:24:43.387+00:00

Hi,

I am working on an ASP.Net core application where one of the actions is responsible to upload large files. The Limits.MaxRequestBodySize property is set to 100MB in the Startup.cs for Kestrel. The action that uploads the file is already decorated with [DisableRequestSizeLimit] and [RequestFormLimits(MultipartBodyLengthLimit = int.MaxValue)].

Despite ignoring the limit at action level and setting the maximum body size to 100MB, the request fails with 500: BadHttpRequestException: "Request body too large" when the file I am trying to upload is only 34MB. The exception occurs in one of the middleware at "await context.Request.Body.CopyToAsync(stream)". And the exception stack trace also mentions the Content-length is 129MB. The exception does not occur if I set the Limits.MaxRequestBodySize to 200MB or to null.

Questions:

  1. Why is the request size 129MB when I am uploading only 34MB file? what makes the remaining ~100MB?
  2. When the request is already in context.Request.Body, why is it throwing error while copying it ("await context.Request.Body.CopyToAsync(stream)")to a new stream?

I really appreciate any help with these. Please let me know if anything is unclear, I can provide more details.

Regards,
Siva

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,555 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.