我在C#中有用于gRPC的服务器和客户端。
我的服务器在Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddGrpc(options =>
{
options.EnableDetailedErrors = true;
options.MaxReceiveMessageSize = 10 * 1024 * 1024; // 2 MB
options.MaxSendMessageSize = 10 * 1024 * 1024; // 5 MB
options.ResponseCompressionLevel = System.IO.Compression.CompressionLevel.Optimal;
options.ResponseCompressionAlgorithm = "gzip";
});
}
将鼠标悬停在ResponseCompressionAlgorithm
上时,会显示以下工具提示文本:
它说我必须在客户端中以"grpc-accept-encoding"
作为值添加头文件gzip
,但在其他论坛上,他们说您只需要在客户端上添加头文件{{1} }。
在我的客户中,我有以下内容:
"grpc-internal-encoding-request"
如果我没有发送 string s = GetRootCertificates();
var channel_creds = new SslCredentials(s);
Channel channel = new Channel("localhost:5001", channel_creds, new ChannelOption[] {
new ChannelOption(ChannelOptions.MaxReceiveMessageLength, 10 * 1024 * 1024), //plenty of room
});
var invoker = channel.Intercept(new ClientLoggerInterceptor());
var client = new Greeter.GreeterClient(invoker);
var headers = new Metadata();
headers.Add("grpc-internal-encoding-request", "gzip");
//headers.Add("grpc-accept-encoding", "gzip");
headers.Add("requester", "cliente");
// deadline is the timeout in sedconds
var callOptions = new CallOptions().WithHeaders(headers).WithDeadline(DateTime.UtcNow.AddSeconds(15)).WithWaitForReady(false);
DataSetResponse response = await client.GetPatientInfoAsync(new PatientInfoRequest { Uid = "DF24387B-815D-4E06-AD76-9E9E3C255118", LoadContractsOnDemand = false }, callOptions);
标头,而是发送"grpc-internal-encoding-request"
,则会收到以下异常:
无论如何,对于
"grpc-accept-encoding"
,我没有任何异常,但是我需要确保压缩确实在进行。如何记录压缩前后的大小?
第二件事就是确保我做对了。