IAsyncEnumerable<T> Streaming

The Memory Trap of Materialization

During a recent performance audit of our reporting APIs, I tracked down erratic Gen2 GC pauses to a seemingly innocent endpoint. The code called .ToListAsync() on a massive table containing over 50,000 historical transaction records before returning an Ok(list) response.

This approach forces ASP.NET Core to allocate the entire result set in memory, doubling or tripling allocations during JSON serialization and locking up thread pool resources.

Before

[HttpGet("reports")] public async Task<IActionResult> GetReports() { var list = await db.Orders.ToListAsync(); return Ok(list); // buffers all rows in RAM } 

After

By leveraging IAsyncEnumerable<T> straight from Entity Framework Core, we bypass the intermediate list allocation entirely. The ASP.NET Core framework understands this interface and immediately streams the records down the HTTP response pipe as data flows from the underlying SQL Server data reader.

[HttpGet("reports")] public IAsyncEnumerable<Order> GetReports() { return db.Orders.AsAsyncEnumerable(); } 

Why It Matters

  • Heap Allocations: Bypasses temporary List<T> backing arrays, keeping memory usage flat regardless of row count.
  • Time-to-First-Byte: The client receives the first JSON chunk instantly instead of waiting for the full query to materialize on the server.
  • Concurrency: Frees up worker threads from waiting on massive memory allocations during peak traffic.

Watch Out

If an exception occurs halfway through reading the stream, your DbContext instance may already be disposed depending on how the request scope handles asynchronous disposal. Ensure your middleware handles mid-stream failures gracefully without corrupting HTTP headers.

How do you handle large data exports in your current architecture?