Streaming EF Core Query Results with IAsyncEnumerable to Cut Memory Use

A performance audit of reporting APIs traced erratic Gen2 garbage collection pauses to an endpoint that loaded over 50,000 transaction records into memory with ToListAsync before returning them. The buffered approach drove up heap allocations and tied up thread pool resources during JSON serialization. Switching the endpoint to return IAsyncEnumerable via AsAsyncEnumerable lets ASP.NET Core stream rows to the client as they are read from the SQL Server data reader. The author reports flatter memory usage, faster time-to-first-byte, and better concurrency under peak load. The article also cautions that mid-stream exceptions may occur after the DbContext is disposed, so middleware must handle such failures without corrupting HTTP headers.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in