protobufsyntax = "proto3"; option csharp_namespace = "Namespace"; package PackageName; //导入其他proto import "google/protobuf/wrappers.proto"; import "google/protobuf/duration.proto"; import "google/protobuf/timestamp.proto"; import "google/protobuf/any.proto"; message DataType{ //值序号无意义 double dou = 0x01; //double //可以为16进制 float fl = 2; //float bool bl = 11; //bool string str = 12; //string uint32 u32 = 3; //uint uint64 u64 = 4; //ulong sint32 s32 = 5; //int sint64 s64 = 6; //long int32 i32=55; //int,建议使用sint32代替 int64 i64=56; //long,建议使用sint64代替 //fixed总使用相同的长度 fixed32 f32 = 7; //uint fixed64 f64 = 8; //ulong sfixed32 sf32 = 9; //int sfixed64 sf64 = 10; //long bytes bs=13; //Google.Protobuf.ByteString google.protobuf.Timestamp time = 14; //utc时间 google.protobuf.Duration duration = 15; string guid=16; //45a9fda3-bd01-47a9-8460-c1cd7484b0b3 //不要使用bytes google.protobuf.DoubleValue dv = 17; //double? google.protobuf.FloatValue fv = 18; //float? google.protobuf.Int32Value i32v = 19; //int? google.protobuf.Int64Value i64v = 20; //long? google.protobuf.UInt32Value u32v = 21; //uint? google.protobuf.UInt64Value u64v = 22; //ulong? repeated string aliases = 23;//Google.Protobuf.Collections.RepeatedField<string> : IList<T> reserved 24, 25 to 28, 31;//保留字段24,25-29,31 用于不再使用的字段,不可再定义这些字段 //不能使用repeated,可以包装在message中,然后对message使用repeated map<string, double> prices =29;//Google.Protobuf.Collections.MapField<string, double> : IDictionary<TKey,TValue> } //嵌套定义 message Outer { message Inner { string text = 1; } Inner inner = 1;//var inner = new Outer.Types.Inner { Text = "Hello" }; } //Any 和 oneof message Stock { // Stock-specific data } message Currency { // Currency-specific data } message ChangeNotification { int32 id = 1; google.protobuf.Any ai = 4;//if(change.ai.Is(Stock.Descriptor)) change.ai.Unpack<Stock>(); oneof oef { //字段编号不能重复 //不能使用repeated,可以包装在message中,然后对message使用repeated Stock stock = 2; Currency currency = 3; } //if(change.OefCase==ChangeNotification.OefOneofCase.Stock) FormatStock(change.Stock); } //枚举 //可嵌套在消息中 //不能使用 Pending | Active这种, 不理解位运算 enum AccountStatus {//字段名称以枚举名称的snake_case开头生成后忽略这一部分 option allow_alias = true;//声明可以有相同值的字段,如没有重复的需要删除 ACCOUNT_STATUS_UNKNOWN = 0;//Unknown = 0,//必须有0作为第一字段 ACCOUNT_STATUS_PENDING = 1;//Pending = 1, ACCOUNT_STATUS_ACTIVE = 2;//Active = 2, ACCOUNT_STATUS_SUSPENDED = 3;//Suspended = 3, ACCOUNT_STATUS_CLOSED = 4;// Closed = 4 ACCOUNT_STATUS_Exit = 4; } // 服务 service Greeter { rpc SayHello (HelloRequest) returns (HelloReply);//答复 } service SimpleStockTicker { rpc Subscribe (HelloRequest) returns (stream HelloReply);//服务器流 } service FullStockTicker { rpc Subscribe (stream HelloRequest) returns (stream HelloReply);//双工流 } message HelloRequest { string name = 1; } message HelloReply { string message = 1; }
.Net Core 中 gRPC Protobuf 数据类型与示例
867
发布于 2020-09-22
更新于 4 年前
C# .Net Core 中 Protobuf 数据类型与 C# 数据类型的对照以及简单的Service示例。