Google.Protobuf 不允许属性为 null

SHM*_*SHM 5 protocol-buffers protobuf-c grpc asp.net-core

我在 ASPNETCore 之上使用 GRPC,也有这样的模型:

syntax = "proto3";
message Blob {
    string id = 1;
    string path = 2;
}
Run Code Online (Sandbox Code Playgroud)

问题是,当我尝试将该path属性设置为 null 时,它会抛出一个ArgumentException.

只需运行此代码:

new Blob { Path = null };
Run Code Online (Sandbox Code Playgroud)

结果如下:

System.ArgumentNullException: Value cannot be null. (Parameter 'value')
   at Google.Protobuf.ProtoPreconditions.CheckNotNull[T](T value, String name)
   at Grpc.Blob.set_Path(String value)
Run Code Online (Sandbox Code Playgroud)

Mar*_*ell 8

Protobuf 没有 null 的概念;nullproto3 将 en 空字符串视为“默认,不发送”,这意味着和之间没有有效负载差异""。所以:也许不要尝试发送null

或者:protobuf-net(通过 protobuf-net.Grpc 与 gRPC 一起正常工作,它仅附加到 Google/Microsoft 位)在这里也可以正常工作,并且由于它不与 proto3 绑定,因此可以对其进行不同null""处理(""显式作为零长度字符串发送并且null根本不发送)。您不需要 protobuf-net 的 .proto,但如果您有: https: //protogen.marcgravell.com/