'.NET 5 Web API performs terribly slow for response of size > 500 KB
I work on a mid-size .NET Web API application. We recently migrated it to .NET 5 from .NET Framework 4.6. Now all the endpoints returning data of size more than 500 KB are terribly slow. It works fine when started locally in Debug as well as Release mode but becomes slow when started on actual servers. We have one endpoint which used to stream 40MB+ data in about 20-40 seconds which now takes 5-6 minutes to stream the same amount of data. Chrome dev tools suggest the delay is because of content download time. I tried converting slower endpoints to use async-await, return IAsyncEnumerable but no luck.
I cannot paste the code here because of strict organization policies. We have an exception handler middleware, certificate verification middleware, and a custom authorization attribute that are functioning properly. We have JSON serializer, response compression, and swagger enabled in StartUp.cs. We are using Self Contained strategy for our deployments. Also, the endpoint does not perform any heavy operations on the data. They just query the database and return it to the client. It is evident that there is no delay in DB operations because the endpoint streaming 40MB+ data stream it from the in-memory cache.
I know it is hard to provide suggestions without having a look at the code. But I would really appreciate it if anyone has faced a similar issue in the past and resolved it using some way.
Solution 1:[1]
One thing I observed in .NET 5 is it automatically adds Transfer-encoding=chunked header in response headers. Disabling the default chunking significantly improved the performance.
This is the link to the discussion on how to disable response chunking in .NET 5: Disable chunking in ASP.NET Core
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Abhishek Kuvalekar |
