.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

相关推荐
Murphy20231 天前
.NetCore项目使用EF Core操作SQL Server
.netcore
码界奇点2 天前
基于.NET Core的CMS内容管理系统设计与实现
c++·毕业设计·.netcore·源代码管理
猹叉叉(学习版)2 天前
【ASP.NET CORE】 13. DDD初步实现
笔记·后端·架构·c#·asp.net·.netcore
武藤一雄2 天前
WPF Command 设计思想与实现剖析
windows·微软·c#·.net·wpf·.netcore
武藤一雄2 天前
WPF 资源解析:StaticResource & DynamicResource 实战指南
微软·c#·.net·wpf·.netcore
武藤一雄2 天前
WPF UI 开发深度指南:资源 (Resources)、样式 (Style) 与触发器 (Trigger) 全解析
ui·c#·.net·wpf·.netcore·avalonia
吹牛不交税2 天前
vue3项目部署到阿里云Alibaba Cloud Linux3系统的docker
docker·容器·.netcore
猹叉叉(学习版)3 天前
【ASP.NET CORE】 12. DDD基本概念
笔记·后端·架构·c#·asp.net·.netcore
江沉晚呤时3 天前
C# 接口默认实现与依赖注入实战指南:.NET 9 企业级开发高级技巧
c#·log4j·.net·.netcore
宝桥南山4 天前
Microsoft Fabric - 试一下在Blazor应用中使用 GraphQL API去连接Lakehouse
microsoft·c#·asp.net·.netcore·fabric·db