dotnet protocol_error ai_generated true

Grpc.Core.RpcException:Status(StatusCode="InvalidArgument", Detail="无效消息长度:收到 1048577 字节,最大为 1048576。")

Grpc.Core.RpcException: Status(StatusCode="InvalidArgument", Detail="Invalid message length: received 1048577 bytes, maximum is 1048576.")

ID: dotnet/grpc-invalid-message-length

其他格式: JSON · Markdown 中文 · English
85%修复率
87%置信度
1证据数
2023-08-01首次发现

版本兼容性

版本状态引入弃用备注
Grpc.Core 2.46 active
Grpc.Net.Client 2.52 active
.NET 6.0 active
.NET 7.0 active
.NET 8.0 active

根因分析

gRPC 请求或响应消息超过了服务器或客户端配置的最大大小限制,默认值为 1 MB(1048576 字节)。

English

A gRPC request or response message exceeds the maximum size limit configured on the server or client, defaulting to 1 MB (1048576 bytes).

generic

官方文档

https://learn.microsoft.com/en-us/aspnet/core/grpc/configuration

解决方案

  1. 在服务器端 Program.cs 中增加最大消息大小:services.AddGrpc(options => options.MaxReceiveMessageSize = 2 * 1024 * 1024); // 2 MB
  2. 在客户端设置 GrpcChannelOptions 上的 MaxReceiveMessageSize:var channel = GrpcChannel.ForAddress("https://localhost:5001", new GrpcChannelOptions { MaxReceiveMessageSize = 2 * 1024 * 1024 });

无效尝试

常见但无效的做法:

  1. 80% 失败

    This removes all size limits, potentially leading to memory exhaustion or denial-of-service attacks.

  2. 60% 失败

    Compression reduces size but may not bring it under the limit if the original is too large; also requires server support.