gRPC 在 protobuf 中使用 Lists

gRPC use of Lists in protobuf

我的服务器端有一个 List<MyClass>,我想使用 gRPC 将它发送给我的客户。 MyClass 如下所示:

public class MyClass
    {
        public long Value { get; set; }
        public DateTime Time { get; set; }
        public string Name { get; set; }
    }

如何在 protobuf 中定义此列表以及我应该以哪种方式覆盖生成的方法?

我的 .proto 目前看起来像这样:

syntax = "proto3";
 
import "google/protobuf/timestamp.proto";

service MyService {
    rpc GetValues(EmptyRequest) returns (MyResponse);
}

message EmptyRequest{
}

message MyResponse{
    repeated int64 values = 1;
    repeated google.protobuf.Timestamp time = 2;
    repeated string name = 3;
}

根消息是单数的(除非您正在使用流式传输),因此您通常会创建一个单独的消息,该消息 具有 list/repeated 组 MyClass.

如果您使用的是 .proto,这可能是:

service MyService {
    rpc GetValues(EmptyRequest) returns (MyResponse);
}

message EmptyRequest{
}

message MyResponse {
    repeated MyClass items = 1;
}
message MyClass {
    int64 values = 1;
    google.protobuf.Timestamp time = 2;
    string name = 3;
}

请注意,您可能还想使用 empty.proto 而不是您自己的。最后,请注意,如果您使用的是 protobuf-net.Grpc,则可以完全跳过 .proto 步骤,只使用您自己的 POCOs