Net Core教程

gRPC Net Core服务器+WPF客户端

本文主要是介绍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类型
doubledoubledoublefloatfloat64Floatdoublefloatdouble
floatfloatfloatfloatfloat32Folatfloatfloatdouble
int32int32intintint32Fixnum or Bignum(as required)intintegerint
int64int64longint/long[3][3]int64Bignumlonginteger/string[5][5]int64
uint32uint32int[1][1]int/long[3][3]uint32Fixnum or Bignum(as required)uintintegerint
uint64uint64long[1][1]int/long[3][3]uint64Bignumulonginteger/string[5][5]}$int64
sint32int32intintint32Fixnum or Bignum(as required)intintegerint
sint64int64longint/long[3][3]int64Bignumlonginteger/string[5][5]int64
fixed32uint32int[1][1]intint32Fixnum or Bignum(as required)intintegerint
fixed64uint64long[1][1]int/long[3][3]uint64Bignumulonginteger/string[5][5]int64
sfixed32uint32int[1][1]intint32Fixnum or Bignum(as required)intintegerint
sfixed64uint64long[1][1]int/long[3][3]uint64Bignumulonginteger/string[5][5]int64
boolboolbooleanboolboolTrueClass/FalseClassboolbooleanbool
stringstringStringstr/unicode[4][4]stringString(UTF-8)stringstringString
bytesstringByteStringstr[]byteString(ASCII-8BIT)ByteStringstringList<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客户端的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!