1、创建grpc服务器端项目,
2、把项目里的 greet.proto 文件的属性 gRPC Stub Classes 设置为Client and Server,这样,客户端就可以引用,否则,此前度娘无解,
3、greet.proto中的service可以创建多个,例如:
service Greeter { // Sends a greeting rpc SayHello (HelloRequest) returns (HelloReply); rpc SayHello2 (NameVales) returns(NameVales); } service Gr2{ rpc SayH(NameVales) returns(NameVales); }
创建多个service后,需要先编译,再在 startup里加入 :
endpoints.MapGrpcService<Gr2Service>();
greet.proto分析:
syntax = "proto3"; //本项目的命名空间 option csharp_namespace = "GrpcService3"; //本项目的包名 package greet; // 服务 service Greeter { //功能 rpc SayHello (HelloRequest) returns (HelloReply); rpc SayHello2 (NameVales) returns(NameVales); } service Gr2{ rpc SayH(NameVales) returns(NameVales); } // 以下为各种请求/反馈的类型 message HelloRequest { string name = 1; } message HelloReply { string message = 1; } message NameVales{ string name=1; int32 val=2; }
proto支持的数据类型:
.proto 类型 | C++类型 | Java类型 | Python类型[2][2] | Go类型 | Ruby类型 | C#类型 | PHP类型 | Dart类型 |
---|---|---|---|---|---|---|---|---|
double | double | double | float | float64 | Float | double | float | double |
float | float | float | float | float32 | Folat | float | float | double |
int32 | int32 | int | int | int32 | Fixnum or Bignum(as required) | int | integer | int |
int64 | int64 | long | int/long[3][3] | int64 | Bignum | long | integer/string[5][5] | int64 |
uint32 | uint32 | int[1][1] | int/long[3][3] | uint32 | Fixnum or Bignum(as required) | uint | integer | int |
uint64 | uint64 | long[1][1] | int/long[3][3] | uint64 | Bignum | ulong | integer/string[5][5]}$ | int64 |
sint32 | int32 | int | int | int32 | Fixnum or Bignum(as required) | int | integer | int |
sint64 | int64 | long | int/long[3][3] | int64 | Bignum | long | integer/string[5][5] | int64 |
fixed32 | uint32 | int[1][1] | int | int32 | Fixnum or Bignum(as required) | int | integer | int |
fixed64 | uint64 | long[1][1] | int/long[3][3] | uint64 | Bignum | ulong | integer/string[5][5] | int64 |
sfixed32 | uint32 | int[1][1] | int | int32 | Fixnum or Bignum(as required) | int | integer | int |
sfixed64 | uint64 | long[1][1] | int/long[3][3] | uint64 | Bignum | ulong | integer/string[5][5] | int64 |
bool | bool | boolean | bool | bool | TrueClass/FalseClass | bool | boolean | bool |
string | string | String | str/unicode[4][4] | string | String(UTF-8) | string | string | String |
bytes | string | ByteString | str | []byte | String(ASCII-8BIT) | ByteString | string | List<int> |
还可以枚举:
message SearchRequest { string query = 1; int32 page_number = 2; int32 result_per_page = 3; enum Corpus { UNIVERSAL = 0; WEB = 1; IMAGES = 2; LOCAL = 3; NEWS = 4; PRODUCTS = 5; VIDEO = 6; } Corpus corpus = 4; }
4、创建net core wpf项目,NuGet安装 Grpc.Core,
5、引入上面的项目,
6、使用:
//地址一定要加上端口,否则未知错误 var channel =new Channel(@"127.0.0.1:80", ChannelCredentials.Insecure); //前面说到的proto文件属性一定要设置为server+client,否则,此前度娘无解 var client = new GrpcService3.Greeter.GreeterClient(channel); var c = new GrpcService3.Gr2.Gr2Client(channel); var a= c.SayH(new GrpcService3.NameVales()); var reply = client.SayHello2(new GrpcService3.NameVales { Name = "LineZero",Val=22 }); var r2 = client.SayHello(new GrpcService3.HelloRequest { Name = "dddddddddd" });