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/

相关推荐
paopaokaka_luck2 小时前
【360】基于springboot的志愿服务管理系统
java·spring boot·后端·spring·毕业设计
码农小旋风3 小时前
详解K8S--声明式API
后端
Peter_chq3 小时前
【操作系统】基于环形队列的生产消费模型
linux·c语言·开发语言·c++·后端
Yaml44 小时前
Spring Boot 与 Vue 共筑二手书籍交易卓越平台
java·spring boot·后端·mysql·spring·vue·二手书籍
小小小妮子~4 小时前
Spring Boot详解:从入门到精通
java·spring boot·后端
hong1616884 小时前
Spring Boot中实现多数据源连接和切换的方案
java·spring boot·后端
睡觉谁叫~~~5 小时前
一文解秘Rust如何与Java互操作
java·开发语言·后端·rust
2401_865854887 小时前
iOS应用想要下载到手机上只能苹果签名吗?
后端·ios·iphone
AskHarries8 小时前
Spring Boot集成Access DB实现数据导入和解析
java·spring boot·后端
2401_857622668 小时前
SpringBoot健身房管理:敏捷与自动化
spring boot·后端·自动化