Compression when using ASP.NET Core Response.BodyWriter PipeWriter

Viewed 396

When using the Response.BodyWriter property to output JSON from a ASP.NET Core Web API endpoint, it seems the configured compression method is ignored.

Compression is enabled using the recommended services.AddResponseCompression and app.UseResponseCompression calls in Startup.cs, and indeed this works for normal endpoints that return object instances that are later serialized by the appropriate middleware.

However, using the PipeWriter method to write output seems to ignore these settings completely.

Of course, we can write compressed data to the Response.BodyWriter pipe directly, but this appears to involve determining the best compression method to use, setting up a Brotli or Gzip writer, and sending chunks through that - all of this is already done by ASP.NET somewhere, so ideally I'd like to avoid re-implementing all this myself.

Is there a standard way to apply the correct compression to the BodyWriter pipe?

0 Answers
Related