Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
Asp.Net Core

Asp.Net Core: Fix Response Compression Skipping a Streamed Result

- 27.11.26 - ErcanOPAK

⚙️ Asp.Net Core: Fix Response Compression Skipping a Streamed Result

The built-in Response Compression middleware decides whether to compress a response largely by checking the Content-Length header up front, and a result written via IAsyncEnumerable<T> or a manually streamed Stream response typically never sets that header at all, since the total size isn’t known in advance. Compression middleware configured with its defaults silently skips these responses entirely, leaving large streamed payloads uncompressed even though smaller, fully-buffered JSON responses right next to them compress just fine.

🔎 The Problem

app.UseResponseCompression();

app.MapGet("/export", async (HttpContext ctx) => {
    ctx.Response.ContentType = "application/json";
    // no Content-Length set - it's a live stream
    await foreach (var row in GetRowsAsync())
        await JsonSerializer.SerializeAsync(ctx.Response.Body, row);
});
// Response-Encoding header: absent. Payload: fully uncompressed.

✅ Fix: Make Compression Explicit for Streamed Endpoints

  • Register the response’s content type explicitly with options.MimeTypes in AddResponseCompression() and confirm it actually matches the streamed endpoint’s real Content-Type, since an unmatched or missing MIME type is a separate, common reason compression gets skipped.
  • For endpoints where streaming is specifically for memory efficiency on huge payloads, wrap the response body in a GZipStream manually and set Content-Encoding: gzip yourself, rather than relying on the generic middleware’s content-length-based heuristics.
  • Verify with a raw curl -i --compressed request against the endpoint directly — browser DevTools sometimes hide the real wire-level headers, and this is the fastest way to confirm whether compression is actually happening.

🚨 Why This Is Dangerous

  • A reporting endpoint that silently ships uncompressed instead of the 70-80% smaller payload everyone assumed response compression was providing can quietly inflate bandwidth costs and slow down every client on a metered or slow connection, with nothing in the logs flagging it as a regression.

Response compression isn’t a universal safety net — it only catches what it was configured to recognize, and a stream with no declared length is easy to miss entirely.

— Backend Architect

Related posts:

ASP.NET Core: Fix Duplicate Records Created When a Client Retries a POST Because There's No Idempote...

ASP.NET Core: Fix Duplicate Records Created When a Client Retries a POST Because There's No Idempote...

ASP.NET Core: Fix a Hosted Service's StopAsync That Gets Cut Off Before It Finishes Because the Defa...

ASP.NET Core: Fix a Hosted Service's StopAsync That Gets Cut Off Before It Finishes Because the Defa...

.NET Core: Master DI Lifecycles (Transient vs Scoped vs Singleton)

.NET Core: Master DI Lifecycles (Transient vs Scoped vs Singleton)

Views: 5
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

Kubernetes: Fix an HPA Ignoring Custom Metrics From the Adapter
Photoshop: Fix a Layer Comp Forgetting a Group’s Visibility Toggle

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Most Read

  1. 01How to make theater mode the default for Youtube 1,079 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 997 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 996 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 873 views
  5. 05How to select distinct rows in a datatable in C# 868 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 671 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 629 views
  8. 08Average of all values in a column that are not zero in SQL 584 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 561 views
Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
Back to top
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com