question

SivaKarthikNarisetty-8864 avatar image
0 Votes"
SivaKarthikNarisetty-8864 asked JasonPan-MSFT commented

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

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

dotnet-aspnet-core-general
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Can you update your upload file code in the post ? Do not include business information, just upload the code, and finally create a sample project, which is helpful for debugging.

0 Votes 0 ·

Hi Jason, the upload works fine regardless of the file size if I exclude the middleware that does the context.Request.Body.CopyToAsync(stream). I believe the upload method is fine, do you still want to look at the code?

0 Votes 0 ·
JasonPan-MSFT avatar image JasonPan-MSFT SivaKarthikNarisetty-8864 ·

From your description, your code look correct, but there may be some details that may cause an error, so just give us the code in the upload part or the created sample project for our reference, so that we can reproduce the problem and fix it. The sample code I created by myself will most likely not reproduce the problem you described

0 Votes 0 ·

0 Answers