.netcore grpc一元方法详解

一、grpc服务端搭建

打开visual studio--》新建项目--》创建ASP.NET Core gRPC服务。 这里我是用的.NET 6.0做为底层框架,使用该框架支持grpc的功能更全面。令注使用nuget包Grpc.AspNetCore这里我使用的是2.40.0版本。

cs 复制代码
// 创建dollar.proto文件

syntax = "proto3";

import "google/protobuf/empty.proto";
option csharp_namespace = "GrpcProject";

package dollar;

service DollarRpc{
	rpc ServerOne (ServerRequest) returns (ServerResponse);
	rpc ServerTwo (ServerRequest) returns (google.protobuf.Empty);
}

message ServerRequest{
	string name = 1;
	double height = 2;
	int32 age = 3;
	bool flag = 4;
	float x = 5;
	float y = 6;
	float z= 7;
	repeated string departments = 8;
}

message ServerResponse
{
	bool result = 1;
}

创建好.proto文件,在项目文件中配置如下

cs 复制代码
 <ItemGroup>
    <Protobuf Include="Protos\dollar.proto" GrpcServices="Server" ProtoCompile="true" />
  </ItemGroup>

也可以鼠标右键.proto文件属性选择Protobuf compiler 同时配置 gRPC Stub Classes项为Server only;同时Compile为true 进行编译;会自动生成服务端可用的基础代码。

同时需要在program或者startup中进行注册和启用:

cs 复制代码
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);
            builder.Services.AddGrpc();
            var app = builder.Build();
            app.MapGrpcService<DollarService>();
            app.Run();
        }

最后启动服务:

二、grpc客户端调用

  1. 创建客户端程序winform
  2. 引用Grpc.AspNetCore nuget包;里面包含Google.Protobuf、Grpc.AspNetCore.Server.ClientFactory、Grpc.Tool三个子包
  3. 引入服务端.proto文件
  4. 在项目文件中.proto文件为客户端文件,并进行编译生成客户端grpc代码
cs 复制代码
  <ItemGroup>
    <Protobuf Include="Protos\dollar.proto" GrpcServices="Client" ProtoCompile="true" />
  </ItemGroup>
  1. 客户端代码
cs 复制代码
            var channel = GrpcChannel.ForAddress("https://localhost:7188");

            var dollarClient = new DollarRpc.DollarRpcClient(channel);
            ServerRequest request = new ServerRequest
            {
                Name = "test",
                Height = 100.3D,
                Age = 18,
                Flag = true,
                X = 50.32F,
                Y = 40.28F,
                Z = 35.6F,
                Departments =
                { "测试一部", "测试二部", "测试三部","测试四部", "测试五部", "测试六部", "测试七部", "测试八部", "测试九部", "测试十部"
                }
            };
            // 服务方法一
            var response = dollarClient.ServerOne(request);

            // 服务方法二
            Empty empty = dollarClient.ServerTwo(request);

6.启动服务调用执行结果

三、内容概述

这是简易的一元方法执行方式,后续会逐渐延申,尽量面面俱到的阐述所有功能点。

项目百度网盘:

链接:https://pan.baidu.com/s/1xs8HiGaZgiY-SUb6ZkRhGg

提取码:i6ej

相关推荐
geovindu4 小时前
CSharp:Condition Variable Pattern
后端·设计模式·c#·.net·.netcore·条件变量模式·同步型模式
geovindu5 天前
CSharp: Observer Pattern
开发语言·后端·观察者模式·设计模式·c#·.netcore·行为模式
geovindu5 天前
CSharp: Strategy Pattern
开发语言·后端·c#·.net·.netcore·策略模式·行为模式
geovindu5 天前
CSharp: Task Scheduler
开发语言·后端·c#·.net·.netcore
geovindu11 天前
CSharp: Wordcloud
开发语言·后端·c#·.net·.netcore·词云
geovindu11 天前
CSharp: Command Pattern
开发语言·后端·c#·.net·.netcore·命令模式·行为模式
全栈小519 天前
【C#】.net core,静态方法线程安全解析,面试时经常被问的线程安全就藏在里面
安全·c#·.netcore
清风与日月21 天前
Yitter.IdGenerator:高性能分布式唯一ID生成器详解
分布式·c#·.net·.netcore
Lost of 程序猿24 天前
ASP.NET Core 后台任务全景:从 BackgroundService 到 Channel 队列,再到分布式调度
后端·asp.net·.netcore
宝桥南山25 天前
Blazor Web Assembly - 体验一下Authentication State从Server共享给Client
microsoft·微软·c#·asp.net·.net·.netcore