Skip to main content

# tweaking

Brotli

    public class BrotliCompressionProvider : ICompressionProvider
    {
        private readonly CompressionLevel? _compressionLevel;

        public BrotliCompressionProvider(CompressionLevel compressionLevel)
        {
            _compressionLevel = compressionLevel;
        }

        public BrotliCompressionProvider()
        {
        }

        public string EncodingName => "br"; // Must match grpc-accept-encoding

        public Stream CreateCompressionStream(Stream outputStream, CompressionLevel? compressionLevel)
        {
            if (_compressionLevel.HasValue)
                return new BrotliStream(outputStream, compressionLevel ?? _compressionLevel.Value, true);

            else if (!_compressionLevel.HasValue && compressionLevel.HasValue)
                return new BrotliStream(outputStream, compressionLevel.Value, true);

            return new BrotliStream(outputStream, CompressionLevel.Fastest, true);
        }

        public Stream CreateDecompressionStream(Stream stream)
        {
            return new BrotliStream(stream, CompressionMode.Decompress);
        }
    }

To enable this,

Enable

    builder.Services.AddGrpc(options => {
        options.EnableDetailedErrors = true;
        options.MaxReceiveMessageSize = 6291456; // 6 MB
        options.MaxSendMessageSize = 6291456; // 6 MB
        options.CompressionProviders = new List<ICompressionProvider>
        {
            new BrotliCompressionProvider() // br
        };
        options.ResponseCompressionAlgorithm = "br"; // grpc-accept-encoding
        options.ResponseCompressionLevel = CompressionLevel.Optimal; // compression level used if not set on the provider
        options.Interceptors.Add<ExceptionInterceptor>(); // Register custom ExceptionInterceptor interceptor
    });

Pr per service

Title

    services.AddGrpc().AddServiceOptions<MyService>(options =>
        {
            options.MaxReceiveMessageSize = 2 * 1024 * 1024; // 2 MB
            options.MaxSendMessageSize = 5 * 1024 * 1024; // 5 MB
        });

# Enable server reflection

Reflection is helpful if we want to allow discovery of our gRPC definitions from tools like Postman. To enable it:

- install X NuGet package
- `builder.Services.AddGrpcReflection()`

To test it: Open Postman ///TBA