question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom Compression Provider is not being triggered

See original GitHub issue

I created a new Core API Project and I had the Interface and class for custom compression as it states here:

https://github.com/grpc/grpc-dotnet/blob/c48ae4442fdae1881622ad94d02dfda45912493a/src/Grpc.Net.Common/Compression/GzipCompressionProvider.cs

Then in my Startup.cs class I have the following:

services.AddGrpc().AddServiceOptions<GreeterService>(options =>
            {
                options.ResponseCompressionLevel = System.IO.Compression.CompressionLevel.Optimal;
                options.ResponseCompressionAlgorithm = "gzip";
                options.CompressionProviders = new List<ICompressionProvider>();
            });

Then I set a few breakpoints just to see if the new provider is being hit without any success:

image

Any clue on how to make this work? Thanks

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
alexvazquezcommented, Nov 10, 2020

The way I solve it was to add the following code:

services.AddGrpc().AddServiceOptions<GreeterService>(options =>
            {

                options.CompressionProviders = new List<ICompressionProvider>()
                {
                    new GrpcService1.Compression.CustomGzipCompressionProvider(System.IO.Compression.CompressionLevel.Optimal)
                };
            
                options.EnableDetailedErrors = true;
            });

And my custom Gzip compression for now is exactly as the GzipCompressionProvider.


public class CustomGzipCompressionProvider : ICompressionProvider
    {
        private readonly CompressionLevel _defaultCompressionLevel;

        /// <summary>
        /// Initializes a new instance of the <see cref="CustomGzipCompressionProvider"/> class with the specified <see cref="CompressionLevel"/>.
        /// </summary>
        /// <param name="defaultCompressionLevel">The default compression level to use when compressing data.</param>
        public CustomGzipCompressionProvider(CompressionLevel defaultCompressionLevel)
        {
            _defaultCompressionLevel = defaultCompressionLevel;
        }

        /// <summary>
        /// The encoding name used in the 'grpc-encoding' and 'grpc-accept-encoding' request and response headers.
        /// </summary>
        public string EncodingName => "gzip";

        /// <summary>
        /// Create a new compression stream.
        /// </summary>
        /// <param name="stream">The stream that compressed data is written to.</param>
        /// <param name="compressionLevel">The compression level.</param>
        /// <returns>A stream used to compress data.</returns>
        public Stream CreateCompressionStream(Stream stream, CompressionLevel? compressionLevel)
        {
            return new GZipStream(stream, compressionLevel ?? _defaultCompressionLevel, leaveOpen: true);
        }

        /// <summary>
        /// Create a new decompression stream.
        /// </summary>
        /// <param name="stream">The stream that compressed data is copied from.</param>
        /// <returns>A stream used to decompress data.</returns>
        public Stream CreateDecompressionStream(Stream stream)
        {
            return new GZipStream(stream, CompressionMode.Decompress, leaveOpen: true);
        }
    }
0reactions
alexvazquezcommented, Apr 22, 2023

I just solved it! Good now I can get my custom compression tool to work. Now I will have to implement something similar client side using Grpc.Core.

How did you solve it? Thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

Response compression in ASP.NET Core
The response compression middleware allows adding additional compression providers for custom Accept-Encoding header values.
Read more >
Net core 1.1 UseResponseCompression isn't compressing
I think you need to specify the compression provider. Try this: services.AddResponseCompression(options => { options.Providers.
Read more >
Response Compression In ASP.NET Core - Milan Jovanović
You can further customize the available providers by adding custom compression providers if you want to. Compression will default to Brotli ...
Read more >
How to Enable GZIP Compression for Faster Web Pages
Learn how GZIP compression works to deliver web pages to browsers more quickly, and how to activate it on your web server.
Read more >
Express compression middleware
This middleware will never compress responses that include a Cache-Control header with the no-transform directive, as compressing will transform the body.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found