.net core 3.0新增長功能async
建立grpg服務ide
1、服務端工具
using System.Threading.Tasks; using Grpc.Core; using Microsoft.Extensions.Logging; namespace GrpcGreeter { 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) { return Task.FromResult(new HelloReply { Message = "Hello " + request.Name }); } } }
2、客戶端spa
1.Nuget包.net
Grpc.Net.Client 包含 .NET Core 客戶端code
Google.Protobuf 包含適用於 C# 的 Protobuf 消息blog
Grpc.Tools 包含適用於 Protobuf 文件的 C# 工具支持。 運行時不須要工具包,所以依賴項標記爲 PrivateAssets="All"
rpc
2.建立Protos文件夾,從 gRPC Greeter 服務將 Protos\greet.proto 文件複製到 gRPC 客戶端項目get
3.工程文件添加string
<ItemGroup> <Protobuf Include="Protos\greet.proto" GrpcServices="Client" /> </ItemGroup>
4.Program.cs
using System; using System.Threading.Tasks; using GrpcGreeter; using Grpc.Net.Client; namespace GrpcGreeterClient { class Program { static async Task Main(string[] args) { var channel = GrpcChannel.ForAddress("https://localhost:5001"); 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(); } } }