gRPC Net Core服务器+WPF客户端
2021/12/24 23:10:19
本文主要是介绍gRPC Net Core服务器+WPF客户端,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
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" });
这篇关于gRPC Net Core服务器+WPF客户端的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-12-06使用Microsoft.Extensions.AI在.NET中生成嵌入向量
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#