'Calling time-consuming API raises error `Unexpected error occured while uploading the file`
I have created an API using ASP.NET Core 6 to do some work. This post API is getting a file using IFormFile, reads it via OpenReadStream, then process the content and returns Ok or BadRequest.
My problem is that processing the request takes about 5 minutes but the browser does not wait more than 2 minutes. It generates error after 2 minutes. However, from the server log, I know that processing the request is not interrupted and is completed to end.
The uploading file is near 1 MB and the error which browser shows is Unexpected error occured while uploading the file. Using Hangfire, cron job and similar work-arounds are not desirable options. Also consider that the front-end is React and the back-end is hosted on docker image. I have tested it on both Chrome and Firefox. Same error happens in both. Also developer console does not show anything helpful. Response is empty, status is empty or showing (failed) net::ERR_EMPTY_RESPONSE (Chrome). Also good to mention that this API does not show error when running locally and is called by Postman, the Postman waits until the end, not matter how long it takes. The production server is protected by CloudFlare (not 100 percent sure).
Firstly, I'm curious to know who is raising this error, web server, front-end, browser, cloud-flare or who?
Secondly, is there any work-around, especially in back-end?
The solutions I have tested so far (which not resolved the issue obviously):
- Separated reading the stream from processing the actual content.
- Adding
.UseKestrel(x => x.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(15))toCreateDefaultBuilderinprogram.cshoping that increasing this item can resolve the issue. - Using different browsers.
Any help is highly appreciated.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
