skip to content

In ASP.NET Core, what does IHttpResponseBodyFeature.DisableBuffering change for a Server-Sent Events stream on Kestrel, on IIS and under response compression?

level: seniorimportance: should knowfreq 28%

answer

  1. a no-op on one server
  2. IIS: native buffering and dynamic compression
  3. compression body: flush every write or skip
  4. Content-Encoding: identity skips compression
  5. call it before the first write

basics

~20 s

On Kestrel nothing: its DisableBuffering is empty. On IIS in-process it turns off native response buffering and dynamic compression. Under the response compression middleware it makes compression flush every write, or skips it; Content-Encoding: identity skips compression entirely.

solid answer

~40 s

`DisableBuffering` asks whichever component owns the response body to pass writes straight through, and each owner answers differently. Kestrel's implementation is empty, because Kestrel sends whatever you flush. IIS in-process calls native `HttpDisableBuffering` and disables IIS dynamic compression for that response, which is what fixes build events arriving in bursts behind IIS. The response compression middleware's body wrapper either flushes the compressor on every write or, if the provider cannot flush, skips compression; it must be called before the first write. `TypedResults.ServerSentEvents` calls it and also sets `Content-Encoding: identity`, and the middleware never compresses a response that already has `Content-Encoding`. Note that `text/event-stream` is not in `ResponseCompressionDefaults.MimeTypes`. None of this helps if a middleware swapped `Response.Body` for a `MemoryStream`.

code

csharp · 11 lines
csharp
builder.Services.AddResponseCompression(options =>
{
    options.EnableForHttps = true;
    // text/event-stream is NOT in the defaults; adding it is what starts the trouble.
    options.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(new[] { "text/event-stream" });
});

// In a hand-written SSE handler, before the first write:
context.Response.ContentType = "text/event-stream";
context.Response.Headers.ContentEncoding = "identity";   // compression middleware skips the response
context.Features.GetRequiredFeature<IHttpResponseBodyFeature>().DisableBuffering(); // IIS: no native buffering

go deeper

for a junior

Know that some hosts and middleware buffer responses and that DisableBuffering asks them to send each write immediately.

for a middle

Explain that Kestrel ignores DisableBuffering, while IIS and the compression body honour it, and that it must come before the first write.

for a senior

Diagnose clumped events by walking the body owners: TypedResults versus hand-written, compression MimeTypes and Content-Encoding, IIS, and body-swapping middleware.

for a principal

Set a policy for streaming endpoints across hosts and middleware so that compression and logging never silently break real-time delivery.

## The symptom The build dashboard works on a developer machine: steps appear one by one. Deployed behind IIS, or after someone enabled response compression for `text/event-stream`, the same endpoint delivers events in bursts, or only when the build ends. The server-side code did not change, so the cause is something between `Response.Body` and the network that holds bytes back. In ASP.NET Core, the switch that tells those layers to stop is `IHttpResponseBodyFeature.DisableBuffering()`. ## One method, several owners `IHttpResponseBodyFeature` is the feature that represents the response body. Each server, and each middleware that wraps the body, supplies its own implementation: | Body owner | What `DisableBuffering()` does | |---|---| | Kestrel | nothing: the method body is empty; flushed bytes are already sent | | IIS in-process | calls native `HttpDisableBuffering` and disables IIS dynamic compression for this response | | HTTP.sys | nothing at present (the source carries a TODO about native buffering) | | `ResponseCompressionMiddleware` body | flushes the compressor on every write, or skips compression if the provider cannot flush, then forwards the call | Two consequences follow: - On **Kestrel** a stream that flushes each event is already incremental; `DisableBuffering` is not what makes it work there. - On **IIS** and under **response compression** it is essential, and it must be called **before the first write**, as the compression body's own comment says. ## How response compression interacts `ResponseCompressionMiddleware` decides per response whether to compress. Relevant rules from its provider: - A response that already has a `Content-Encoding` header is **not** compressed. - Only content types in `ResponseCompressionOptions.MimeTypes` are compressed; the defaults (`ResponseCompressionDefaults.MimeTypes`) contain types such as `text/plain`, `application/json` and `text/html`, but **not** `text/event-stream`. - Over HTTPS nothing is compressed unless `EnableForHttps` is set. So by default an SSE stream is not compressed at all. Trouble starts when someone adds `text/event-stream` to `MimeTypes`. Then a hand-written stream that never calls `DisableBuffering` gets compressed by a stream that collects output until it has enough to compress. `TypedResults.ServerSentEvents` avoids this by setting `Content-Encoding: identity` and calling `DisableBuffering()` itself. ## Where the call travels `DisableBuffering` is a chain, not a single switch. When `UseResponseCompression()` sits in front of the endpoint, `HttpContext.Features` returns the compression wrapper's `IHttpResponseBodyFeature`. Its `DisableBuffering` first makes its own decision (auto-flush, or skip compression when the chosen provider's `SupportsFlush` is false; the built-in gzip and Brotli providers both report `true`), then forwards the call to the feature it wrapped, which is the server's. So a single call from the SSE handler reaches every owner that implements the feature in the chain. A layer that bypasses the feature, by replacing `Response.Body` with its own stream, is invisible to the call. ## What DisableBuffering cannot fix - **A middleware that replaced `Response.Body`** with a `MemoryStream`, for example to log response bodies, holds everything until the request ends; it does not implement the feature, so the call never reaches it. Exclude streaming endpoints from such middleware. - **Writes that are never flushed**, such as a `StreamWriter` without `FlushAsync`. - **Reverse proxies and CDNs** that buffer on their own; those need their own configuration. ## A checklist for a clumping SSE endpoint 1. Is the stream returned through `TypedResults.ServerSentEvents`, or does the hand-written version call `DisableBuffering()` before the first write? 2. Has anyone added `text/event-stream` to the compression `MimeTypes`? If so, set `Content-Encoding: identity` on the stream. 3. Is the app hosted in IIS, where `DisableBuffering` is the documented switch? 4. Does any custom middleware swap `Response.Body`? 5. Only then look outside the process at proxies.

  • Why does the response compression middleware's DisableBuffering sometimes skip compression instead of flushing?
    Its body wrapper checks the selected provider's `SupportsFlush`. A compressor that cannot flush would hold real-time output until the response ends, which the source comment notes would block responses like SignalR's. So for such a provider it marks the response as already checked and sends it uncompressed; for providers that can flush it sets auto-flush, so every write is compressed and pushed out immediately.

  • Kestrel buffers SSE output until DisableBuffering is called.
  • DisableBuffering can be called after the first event and still takes effect.
  • text/event-stream is in ResponseCompressionDefaults.MimeTypes, so SSE is compressed by default.
  • DisableBuffering also stops a logging middleware's MemoryStream from holding the body.
  • Setting EnableForHttps to false is how you keep SSE uncompressed on HTTP.