• Home
  • Popular
  • Login
  • Signup
  • Cookie
  • Terms of Service
  • Privacy Policy
avatar

Posted by User Bot


28 Feb, 2025

Updated at 24 Mar, 2025

Ensure .NET GRPC Compression is correctly working

In an .NET 9 GRPC application, MaxSendMessageSize and MaxReceiveMessageSize have been set to a 256K and compression is not used by default. However, there is one unary call whose response is greater and throws on the client. This call is being used just once in the client to download some data. I don't want to increase MaxSize only because of this, so I would like to use compression for only this specific call.

I followed the relevant example (https://github.com/grpc/grpc-dotnet/tree/master/examples/Compressor) and this is the F# code

//Server

        services.AddGrpc(fun options -> 
            options.EnableDetailedErrors <- true
            options.ResponseCompressionLevel <- IO.Compression.CompressionLevel.SmallestSize
            options.ResponseCompressionAlgorithm <- "gzip"
            options.MaxSendMessageSize <- Constants.MaxGrpcMessageSize )

//Client

//Channel Setup
        let channel = GrpcChannel.ForAddress(address,
            GrpcChannelOptions(
                HttpHandler = new SocketsHttpHandler(MaxConnectionsPerServer = 1, InitialHttp2StreamWindowSize = Constants.Http2InitialStreamWindowSize (*1MB*)),
                MaxReceiveMessageSize = Constants.MaxGrpcMessageSize,
                CompressionProviders = [|GzipCompressionProvider(IO.Compression.CompressionLevel.SmallestSize)|]))

//Call
        let metadata = Metadata()
        metadata.Add("grpc-internal-encoding-request", "gzip")

        let! reply = client.GetProductsAsync(GetProductsRequest(ExchangeId = marketId), metadata)

However the exception (message exceeds max size) is still thrown.

One possibility is that the message is still too big once compressed, but to ascertain that I should ensure that compression is applied, and the example code I referred to does not show how. This is what I tried

//Client response

            let response = client.GetProductsAsync(GetProductsRequest(ExchangeId = marketId), metadata)
            let! headers = response.ResponseHeadersAsync
            let! reply = response.ResponseAsync
            return Some reply

expecting to see a "grpc-.." something header, but could never see any.

1) What can I do verify (possibly in code) and ensure that compression is applied?

2) What else could be missing in the code to make GRPC api apply compression for this response?