一、.proto文件字段概述
- grpc的接口傳輸參數都是根據.proto文件約定的字段格式進行傳輸的
- grpc提供了多種類型字段;主要包括標量值類型(基礎類型)、日期時間、可為null類型、字節、列表、字典、Any類型(任意類型)、Oneof等
- 字段嚴格規范,是一種強類型文件協議
二、案例介紹
- 標量值類型
- 日期時間
- 可為null類型
- 字節
- 列表
- 字典
- Any類型
- Oneof:一種語言特性,可以通過該特性進行對象切換處理;使用?
oneof
?指定可能返回 A對象或B對象?的響應消息
三、服務端定義
- 定義通用消息實體
- 根據不同的類型創建對應的案例實體
- 特殊的字段需要導入指定的包才能使用
- 定義引用字段的各個服務接口
- 內部實現邏輯,及打印展示相應的字段結果
// 1.提供公共的實體proto文件
// 2.服務引用對應的proto文件// 通用消息文件datamessages.proto
syntax = "proto3";option csharp_namespace = "GrpcProject";package grpc.serviceing;// 基礎類型實體
message BaseConfig{string name = 1;double position = 2;float distance= 3 ;int32 age = 4;int64 timeSpanId = 5;uint32 uAge = 6;uint64 uTimeSpanId = 7;sint32 sAge = 8;sint64 sTimeSpanId = 9;bool flag = 10;
}// 日期類型實體 需要導入 日期namespace
import "google/protobuf/duration.proto";
import "google/protobuf/timestamp.proto";message DateConfig{int64 id = 1;google.protobuf.Timestamp dateTimestamp = 2;google.protobuf.Duration dateDuration = 3;
}// 字節類型
message ByteConfig{int64 id = 1;bytes positionBytes = 2;
}// 可為null類型 需要導入 null的namespace
import "google/protobuf/wrappers.proto";message NullConfig{int64 id = 1;google.protobuf.BoolValue nullBool = 2;google.protobuf.DoubleValue nullDoubule = 3;google.protobuf.FloatValue nullFloat = 4;google.protobuf.Int32Value nullInt = 5;google.protobuf.Int64Value nullLong = 6;google.protobuf.UInt32Value nullUInt = 7;google.protobuf.StringValue nullString = 8;google.protobuf.BytesValue nullBytes = 9;
}// 集合類型
message ListConfig{int64 id = 1;// 數組repeated string names = 2;repeated ListDetailConfig details = 3;// 字典map<int32,string> attributes = 4;map<int32,ListDetailConfig> dicDetail = 5;
}message ListDetailConfig{string name = 1;int32 height = 2;
}// 任意類型 需要導入對應的包
import "google/protobuf/any.proto";
message AnyConfig{int32 id = 1;google.protobuf.Any anyObject = 2;
}// Oneof類型 編譯器在生成消息類時處理 oneof 關鍵字。 使用 oneof 指定可能返回 A 或 B 或 C 的響應消息
message OneofConfig{oneof result{A oA = 1;B oB = 2;C oC = 3;}
}message A{int32 id = 1;
}message B{int32 id = 1;
}message C{int32 id =1;
}
// 接口服務定義protofield.proto文件syntax = "proto3";import "google/protobuf/empty.proto";
import "Protos/datamessages.proto";option csharp_namespace = "GrpcProject";package grpc.serviceing;service FieldRpc{// 基礎字段處理rpc BaseConfigService(BaseConfig) returns (google.protobuf.Empty);// 日期字段處理rpc DateConfigService(DateConfig) returns (google.protobuf.Empty);// 字節處理rpc ByteConfigService(ByteConfig) returns (google.protobuf.Empty);// null字段處理rpc NullConfigService(NullConfig) returns (google.protobuf.Empty);// 集合類型字段處理rpc ListConfigService(ListConfig) returns (google.protobuf.Empty);// 任意類型字段處理rpc AnyConfigService(AnyConfig) returns (google.protobuf.Empty);// Oneof類型字段處理rpc OneofConfigService(OneofConfig) returns (google.protobuf.Empty);
}
// 服務實現類/// <summary>/// 字段處理服務/// </summary>public class ProtoFieldService : FieldRpc.FieldRpcBase{// 基礎配置public override async Task<Empty> BaseConfigService(BaseConfig request, ServerCallContext context){await Console.Out.WriteLineAsync("\r\n--------------------------基礎配置--------------------------\r\n");// 打印字段信息var properties = request.GetType().GetProperties();foreach (var property in properties){var value = property.GetValue(request);await Console.Out.WriteLineAsync($"{property.Name}:{value}");}return new Empty();}// 日期配置public override async Task<Empty> DateConfigService(DateConfig request, ServerCallContext context){await Console.Out.WriteLineAsync("\r\n--------------------------日期配置--------------------------\r\n");// timspanvar duration = request.DateDuration.ToTimeSpan();await Console.Out.WriteLineAsync($"{nameof(duration)}:{duration.TotalSeconds}");// datetimevar time = request.DateTimestamp.ToDateTime();await Console.Out.WriteLineAsync($"{nameof(time)}:{time:yyyy-MM-dd HH:mm:ss}");return new Empty();}// 字節public override async Task<Empty> ByteConfigService(ByteConfig request, ServerCallContext context){await Console.Out.WriteLineAsync("\r\n--------------------------字節配置--------------------------\r\n");var bytes = request.PositionBytes.ToByteArray();var message = Encoding.UTF8.GetString(bytes, 0, bytes.Length);await Console.Out.WriteLineAsync($"{nameof(message)}:{message}");return new Empty();}// null配置public override async Task<Empty> NullConfigService(NullConfig request, ServerCallContext context){await Console.Out.WriteLineAsync("\r\n--------------------------null配置--------------------------\r\n");// 打印字段信息var properties = request.GetType().GetProperties();foreach (var property in properties){var value = property.GetValue(request);var printValue = value == null ? "返回null值" : value;await Console.Out.WriteLineAsync($"{property.Name}:{printValue}");}return new Empty();}// 集合public override async Task<Empty> ListConfigService(ListConfig request, ServerCallContext context){await Console.Out.WriteLineAsync("\r\n--------------------------集合配置--------------------------\r\n");var id = request.Id;await Console.Out.WriteLineAsync($"主鍵標識:{id}\r\n");// 轉換數組arrayawait Console.Out.WriteLineAsync($"打印names信息:");var names = request.Names.ToArray();foreach (var name in names){await Console.Out.WriteLineAsync(name);}// 轉換listawait Console.Out.WriteLineAsync($"\r\n打印detailList信息:");var detailList = request.Details.ToList();foreach (var item in detailList){await Console.Out.WriteLineAsync($"ListDetailConfig:{nameof(item.Name)} {item.Name};{nameof(item.Height)} {item.Height}。");}// 字典await Console.Out.WriteLineAsync($"\r\n打印一般字典信息:");var dicAttributes = request.Attributes.ToDictionary(t => t.Key, t => t.Value);foreach (var attr in dicAttributes){await Console.Out.WriteLineAsync($"key:{attr.Key};value:{attr.Value}。");}await Console.Out.WriteLineAsync($"\r\n打印對象字典信息:");foreach (var item in request.DicDetail){await Console.Out.WriteLineAsync($"key:{item.Key};value:{item.Value.Name} | {item.Value.Height}。");}return new Empty();}// Anypublic override async Task<Empty> AnyConfigService(AnyConfig request, ServerCallContext context){await Console.Out.WriteLineAsync("\r\n--------------------------Any配置--------------------------\r\n");var anyObject = request.AnyObject;// 檢查是否是A對象if (anyObject.Is(A.Descriptor)){var a = anyObject.Unpack<A>();if (a is not null){await Console.Out.WriteLineAsync($"A對象:{a.Id}");}}else if (anyObject.Is(B.Descriptor)){var b = anyObject.Unpack<B>();if (b is not null){await Console.Out.WriteLineAsync($"B對象:{b.Id}");}}else if (anyObject.Is(C.Descriptor)){var c = anyObject.Unpack<C>();if (c is not null){await Console.Out.WriteLineAsync($"C對象:{c.Id}");}}else{await Console.Out.WriteLineAsync("Any未解析到任何對象");}return new Empty();}// oneofpublic override async Task<Empty> OneofConfigService(OneofConfig request, ServerCallContext context){await Console.Out.WriteLineAsync("\r\n--------------------------Oneof配置--------------------------\r\n");// 檢測對應的對象是否有值switch (request.ResultCase){case OneofConfig.ResultOneofCase.None:await Console.Out.WriteLineAsync("未檢測到任何對象");break;case OneofConfig.ResultOneofCase.OA:await Console.Out.WriteLineAsync($"對象OA存在值:{request.OA.Id}");break;case OneofConfig.ResultOneofCase.OB:await Console.Out.WriteLineAsync($"對象OB存在值:{request.OB.Id}");break;case OneofConfig.ResultOneofCase.OC:await Console.Out.WriteLineAsync($"對象OC存在值:{request.OC.Id}");break;default:break;}return new Empty();}}
三、客戶端定義
- 引用proto文件,配置為客戶端類型
- 根據編譯生成的函數進行傳參調用
- 創建WPF測試客戶端
- 各個服務接口創建對應的按鈕進行調用
- 執行過程中,服務端控制臺會打印對應的消息
// 基礎private async void BtnBaseconfig_Click(object sender, RoutedEventArgs e){await WpfFieldClient.Show(1);MessageBox();}// 日期private async void BtnDateconfig_Click(object sender, RoutedEventArgs e){await WpfFieldClient.Show(2);MessageBox();}// 字節private async void BtnByteconfig_Click(object sender, RoutedEventArgs e){await WpfFieldClient.Show(3);MessageBox();}// nullprivate async void BtnNullconfig_Click(object sender, RoutedEventArgs e){await WpfFieldClient.Show(4);MessageBox();}// listprivate async void BtnListconfig_Click(object sender, RoutedEventArgs e){await WpfFieldClient.Show(5);MessageBox();}// anyprivate async void BtnAnyconfig_Click(object sender, RoutedEventArgs e){await WpfFieldClient.Show(6);MessageBox();}// Oneofprivate async void BtnOneofconfig_Click(object sender, RoutedEventArgs e){await WpfFieldClient.Show(7);MessageBox();}
調用的類庫:
public class WpfFieldClient{/// <summary>/// 根據參數選擇不同的方法執行/// </summary>/// <param name="k"></param>public static async Task Show(int k){var channel = GrpcChannel.ForAddress("https://localhost:7188");var client = new GrpcProject.FieldRpc.FieldRpcClient(channel);// 基礎BaseConfig config = new BaseConfig();config.Name = "張三";config.Position = 2.33D;config.Distance = 5.48F;config.Age = 10;config.TimeSpanId = 6538590027736100;config.SAge = 1921;config.STimeSpanId = 6538590027736130;config.Flag = true;// 日期DateConfig dateConfig = new DateConfig();dateConfig.Id = 179;dateConfig.DateDuration = Duration.FromTimeSpan(TimeSpan.FromSeconds(5));// 注意這里的時間是utc時間dateConfig.DateTimestamp = Timestamp.FromDateTime(DateTime.UtcNow);// 字節ByteConfig byteConfig = new ByteConfig();byteConfig.Id = 9854564654654;byteConfig.PositionBytes = ByteString.CopyFrom(Encoding.UTF8.GetBytes("莊這人的南的很"));// nullNullConfig nullConfig = new NullConfig();nullConfig.Id = 1854564654654;nullConfig.NullBool = true;nullConfig.NullFloat = null;nullConfig.NullUInt = null;nullConfig.NullInt = 15;nullConfig.NullLong = 112345451234787;// ListConfigListConfig listConfig = new ListConfig();var attributes = new Dictionary<int, string>{[1] = "one",[2] = "two",[3] = "three",[4] = "four",[5] = "five"};listConfig.Id = 123456456;listConfig.Attributes.Add(attributes);var dicDetail = new Dictionary<int, ListDetailConfig>{[1] = new ListDetailConfig { Height = 1, Name = "one" },[2] = new ListDetailConfig { Height = 2, Name = "two" },[3] = new ListDetailConfig { Height = 3, Name = "three" },[4] = new ListDetailConfig { Height = 4, Name = "four" },[5] = new ListDetailConfig { Height = 5, Name = "five" }};listConfig.DicDetail.Add(dicDetail);listConfig.Details.Add(new ListDetailConfig { Height = 8, Name = "Eight" });var detailConfigs = new List<ListDetailConfig>{new ListDetailConfig { Height=9,Name="nine"},new ListDetailConfig{ Height=10,Name="ten"}};listConfig.Details.Add(detailConfigs);// AnyAnyConfig anyConfig = new AnyConfig();anyConfig.Id = 42564134;anyConfig.AnyObject = Any.Pack(new B { Id = 15 });// OneofOneofConfig oneofConfig = new OneofConfig();oneofConfig.OA = new A { Id = 1 };//oneofConfig.OC = new C { Id = 2 };var emptyResult = k switch{1 => await client.BaseConfigServiceAsync(config),2 => await client.DateConfigServiceAsync(dateConfig),3 => await client.ByteConfigServiceAsync(byteConfig),4 => await client.NullConfigServiceAsync(nullConfig),5 => await client.ListConfigServiceAsync(listConfig),6 => await client.AnyConfigServiceAsync(anyConfig),7 => await client.OneofConfigServiceAsync(oneofConfig),_ => new Empty()};}}
五、執行結果
服務端:
?客戶端:
?六、源碼地址
鏈接:https://pan.baidu.com/s/150TKY2Kgln3l_uKAsztyzw?
提取碼:hkb9