dotnet protocol_error ai_generated true

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

ID: dotnet/grpc-invalid-message-length

Also available as: JSON · Markdown · 中文
85%Fix Rate
87%Confidence
1Evidence
2023-08-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Grpc.Core 2.46 active
Grpc.Net.Client 2.52 active
.NET 6.0 active
.NET 7.0 active
.NET 8.0 active

Root Cause

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

generic

中文

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

Official Documentation

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

Workarounds

  1. 90% success Increase the maximum message size on the server in Program.cs: services.AddGrpc(options => options.MaxReceiveMessageSize = 2 * 1024 * 1024); // 2 MB
    Increase the maximum message size on the server in Program.cs: services.AddGrpc(options => options.MaxReceiveMessageSize = 2 * 1024 * 1024); // 2 MB
  2. 85% success On the client side, set MaxReceiveMessageSize on GrpcChannelOptions: var channel = GrpcChannel.ForAddress("https://localhost:5001", new GrpcChannelOptions { MaxReceiveMessageSize = 2 * 1024 * 1024 });
    On the client side, set MaxReceiveMessageSize on GrpcChannelOptions: var channel = GrpcChannel.ForAddress("https://localhost:5001", new GrpcChannelOptions { MaxReceiveMessageSize = 2 * 1024 * 1024 });

中文步骤

  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 });

Dead Ends

Common approaches that don't work:

  1. 80% fail

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

  2. 60% fail

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