共计 1316 个字符,预计需要花费 4 分钟才能阅读完成。
Grpc 对象转 proto 代码工具
尽管 Grpc.Tools 能够将 proto 文件主动生成代理类,然而 proto 文件得手敲,还容易出错,如果接口比较复杂,定义比拟多,这就很头疼了
为了解决这个问题 Class2Proto 诞生了,应用规范 C# 对象转换成 proto 文件,不论是新写的接口,还是老的 API 接口转 Grpc, 都没问题
- <u> 装置 nuget 包:CRL.Class2Proto</u>
- <u>using CRL.Class2Proto;</u>
定义标准接口代码
[ProtoServiceAttribute("protoTest", "ClassTestAction")] | |
public interface ClassTestAction | |
{ClassTest getObj(TestObj a); | |
//ClassTest getObj2(TestObj a); | |
Request getObj3(TestObj2<Request> a); | |
TestObj2<List<Request>> getObj4(TestObj2<List<Request>> a); | |
} |
运行转换方法生成 proto 文件
var convertInfo = ClassConvert.Convert(System.Reflection.Assembly.GetAssembly(typeof(ClassTest))); | |
convertInfo.ForEach(b => b.CreateCode()); |
程序目录 Protos 成生了 protoTest.proto 文件
syntax = "proto3"; | |
option csharp_namespace = "gRPC.gRpcClient.protoTest"; | |
package protoTest; | |
service ClassTestAction {rpc getObj(TestObjDTO) returns (ClassTestDTO); | |
rpc getObj3(TestObj2_RequestDTO) returns (RequestDTO); | |
rpc getObj4(TestObj2_l_RequestDTO) returns (TestObj2_l_RequestDTO); | |
} | |
message StatusDTO { | |
ok = 0; | |
fail = 1; | |
} | |
message TestObjDTO {string Id = 1;} | |
message ClassTestDTO { | |
string Name = 1; | |
int32 nullableValue = 2; | |
StatusDTO Status = 3; | |
TestObjDTO Data = 4; | |
repeated string Name2 = 5; | |
repeated TestObjDTO Data2 = 6; | |
map<string, TestObjDTO> Data3 = 7; | |
string time = 8; | |
double decimalValue = 9; | |
string Id = 10; | |
} | |
message RequestDTO { | |
} | |
message TestObj2_RequestDTO { | |
string Id = 1; | |
RequestDTO data = 2; | |
} | |
message TestObj2_l_RequestDTO { | |
string Id = 1; | |
repeated RequestDTO data = 2; | |
} |
正文完