.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

相关推荐
weixin_379880926 天前
.Net Core WebApi集成Swagger
java·服务器·.netcore
The Future is mine8 天前
.Net Core 在Linux系统下创建服务
linux·运维·.netcore
*长铗归来*9 天前
ASP.NET Core Web API 中控制器操作的返回类型及Swagger
后端·c#·asp.net·.netcore
IDOlaoluo9 天前
VS2017 安装 .NET Core 2.2 SDK 教程(包括 dotnet-sdk-2.2.108-win-x64.exe 安装步骤)
.netcore
csdn_aspnet17 天前
使用 Entity Framework Code First 方法创建 ASP.NET Core 5.0 Web API
.netcore·webapi
小先生81217 天前
.NET Core项目中 Serilog日志文件配置
c#·.netcore
爱吃香蕉的阿豪17 天前
.NET Core 中 System.Text.Json 与 Newtonsoft.Json 深度对比:用法、性能与场景选型
数据库·json·.netcore
csdn_aspnet17 天前
ASP.NET Core 10.0 的主要变化
.netcore
csdn_aspnet20 天前
在 C# .NETCore 中使用 MongoDB(第 1 部分):驱动程序基础知识和插入文档
mongodb·.netcore
csdn_aspnet20 天前
在 C# .NETCore 中使用 MongoDB(第 3 部分):跳过、排序、限制和投影
mongodb·c#·.netcore