.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

相关推荐
时光追逐者1 天前
C#/.NET/.NET Core技术前沿周刊 | 第 41 期(2025年6.1-6.8)
c#·.net·.netcore
[email protected]4 天前
ASP.NET Core SignalR - 部分客户端消息发送
后端·asp.net·.netcore
bbsh20994 天前
WebFuture:Ubuntu 系统上在线安装.NET Core 8 的步骤
linux·ubuntu·.netcore·webfuture
MoFe15 天前
【.net core】【watercloud】树形组件combotree导入及调用
.netcore
MoFe15 天前
【.net core】.KMZ文件解压为.KML文件并解析为GEOJSON坐标数据集。附KML处理多线(LineString)闭环问题
.netcore
The Future is mine5 天前
在.NET Core控制器中获取AJAX传递的Body参数
c#·.netcore
无味无感6 天前
ASP.NET Core使用Quartz部署到IIS资源自动被回收解决方案
.netcore
MoFe16 天前
【.net core】天地图坐标转换为高德地图坐标(WGS84 坐标转 GCJ02 坐标)
java·前端·.netcore
步、步、为营7 天前
.NET Core接口IServiceProvider
.net·.netcore
冰茶_9 天前
建造者模式:优雅构建复杂对象
设计模式·微软·c#·.netcore·建造者模式·软件开发