asp.net core grpc快速入门

环境

.net 8

vs2022

创建 gRPC 服务器


一定要勾选Https

安装Nuget包

xml 复制代码
<PackageReference Include="Google.Protobuf" Version="3.28.2" />
<PackageReference Include="Grpc.AspNetCore" Version="2.66.0" />
<PackageReference Include="Grpc.Net.Client" Version="2.66.0" />
<PackageReference Include="Grpc.Tools" Version="2.66.0">
	<PrivateAssets>all</PrivateAssets>
	<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>

新建Protos文件夹,该文件下新建greet.proto

go 复制代码
syntax = "proto3";

service Greeter {
  rpc SayHello (HelloRequest) returns (HelloReply);
}

message HelloRequest {
  string name = 1;
}

message HelloReply {
  string message = 1;
}

添加编译

xml 复制代码
<ItemGroup>
  <Protobuf Include="Protos\greet.proto" GrpcServices="Server" />
</ItemGroup>

新建Service文件夹,该文件夹下新建GreeterService

cs 复制代码
using Grpc.Core;

namespace GrpcServer01.Service
{
    public class GreeterService : Greeter.GreeterBase
    {
        private readonly ILogger<GreeterService> _logger;

        public GreeterService(ILogger<GreeterService> logger)
        {
            _logger = logger;
        }

        public override Task<HelloReply> SayHello(HelloRequest request,
            ServerCallContext context)
        {
            _logger.LogInformation("Saying hello to {Name}", request.Name);
            return Task.FromResult(new HelloReply
            {
                Message = "Hello " + request.Name
            });
        }
    }
}

修改Program.cs

cs 复制代码
using GrpcServer01.Service;

namespace GrpcServer01
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);
            //新增代码在这
            builder.Services.AddGrpc();
            
            var app = builder.Build();
            
            //新增代码在这
            app.MapGrpcService<GreeterService>();
            app.MapGet("/", () => "Hello World!");

            app.Run();
        }
    }
}

启动项目访问https://localhost:7006/

服务端创建成功

创建grpc客户端


安装nuget包

xml 复制代码
<ItemGroup>
	<PackageReference Include="Google.Protobuf" Version="3.28.2" />
	<PackageReference Include="Grpc.Net.Client" Version="2.66.0" />
	<PackageReference Include="Grpc.Tools" Version="2.66.0">
		<PrivateAssets>all</PrivateAssets>
		<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
	</PackageReference>
</ItemGroup>

复制服务端Protos\greet.proto到客户端

xml 复制代码
<ItemGroup>
	<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
</ItemGroup>

修改Program.cs

cs 复制代码
internal class Program
{
    static async Task Main(string[] args)
    {
        using var channel = GrpcChannel.ForAddress("https://localhost:7006");
        var client = new Greeter.GreeterClient(channel);
        var reply = await client.SayHelloAsync(
                          new HelloRequest { Name = "GreeterClient" });
        Console.WriteLine("Greeting: " + reply.Message);
        Console.WriteLine("Press any key to exit...");
        Console.ReadKey();
    }
}

客户端

服务端

参考

https://grpc.io/docs/languages/csharp/
https://blog.csdn.net/kevin_tech/article/details/120681720
https://learn.microsoft.com/zh-cn/aspnet/core/grpc/?view=aspnetcore-8.0
grpc详解
https://cloud.tencent.com/developer/article/1913513
https://protobuf.dev/overview/

相关推荐
凡人的AI工具箱14 分钟前
15分钟学 Go 第 60 天 :综合项目展示 - 构建微服务电商平台(完整示例25000字)
开发语言·后端·微服务·架构·golang
java亮小白199722 分钟前
Spring循环依赖如何解决的?
java·后端·spring
2301_8112743139 分钟前
大数据基于Spring Boot的化妆品推荐系统的设计与实现
大数据·spring boot·后端
草莓base1 小时前
【手写一个spring】spring源码的简单实现--容器启动
java·后端·spring
Ljw...2 小时前
表的增删改查(MySQL)
数据库·后端·mysql·表的增删查改
编程重生之路2 小时前
Springboot启动异常 错误: 找不到或无法加载主类 xxx.Application异常
java·spring boot·后端
薯条不要番茄酱2 小时前
数据结构-8.Java. 七大排序算法(中篇)
java·开发语言·数据结构·后端·算法·排序算法·intellij-idea
qq_174482857510 小时前
springboot基于微信小程序的旧衣回收系统的设计与实现
spring boot·后端·微信小程序
锅包肉的九珍10 小时前
Scala的Array数组
开发语言·后端·scala
心仪悦悦10 小时前
Scala的Array(2)
开发语言·后端·scala