在本文中,你将了解

先决条件创建 gRPC 服务

asp.netcore的服务注册(在ASP.NETCore)(1)

运行服务

asp.netcore的服务注册(在ASP.NETCore)(2)

asp.netcore的服务注册(在ASP.NETCore)(3)

日志显示该服务正在侦听 HTTPS://localhost:5001。

控制台

info: Microsoft.Hosting.Lifetime[0] Now listening on: https://localhost:5001 info: Microsoft.Hosting.Lifetime[0] Application started. Press Ctrl C to shut down. info: Microsoft.Hosting.Lifetime[0] Hosting environment: Development

备注

gRPC 模板配置为使用传输层安全性 (TLS)。 gRPC 客户端需要使用 HTTPS 调用服务器。

macOS 不支持 ASP.NET Core gRPC 及 TLS。 在 macOS 上成功运行 gRPC 服务需要其他配置。 有关详细信息,请参阅无法在 macOS 上启用 ASP.NET Core gRPC 应用。

检查项目文件

GrpcGreeter 项目文件:

在 .NET 控制台应用中创建 gRPC 客户端添加所需的包

gRPC 客户端项目需要以下包:

通过包管理器控制台 (PMC) 或管理 NuGet 包来安装包。

用于安装包的 PMC 选项管理 NuGet 包选项以安装包添加 greet.proto

XML

<ItemGroup> <Protobuf Include="Protos\greet.proto" GrpcServices="Client" /> </ItemGroup>

创建 Greeter 客户端

构建客户端项目,以在 GrpcGreeter 命名空间中创建类型。 GrpcGreeter 类型是由生成进程自动生成的。

使用以下代码更新 gRPC 客户端的 Program.cs 文件:

C#

using System; using System.Net.Http; using System.Threading.Tasks; using Grpc.Net.Client; namespace GrpcGreeterClient { class Program { static async Task Main(string[] args) { // The port number(5001) must match the port of the gRPC server. using 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(); } }

Program.cs 包含 gRPC 客户端的入口点和逻辑。

通过以下方式创建 Greeter 客户端:

C#

static async Task Main(string[] args) { // The port number(5001) must match the port of the gRPC server. using 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(); }

Greeter 客户端会调用异步 SayHello 方法。 随即显示 SayHello 调用的结果:

C#

static async Task Main(string[] args) { // The port number(5001) must match the port of the gRPC server. using 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();

使用 gRPC Greeter 服务测试 gRPC 客户端

gRPC 服务在写入命令提示符的日志中记录成功调用的详细信息:

控制台

info: Microsoft.Hosting.Lifetime[0] Now listening on: https://localhost:5001 info: Microsoft.Hosting.Lifetime[0] Application started. Press Ctrl C to shut down. info: Microsoft.Hosting.Lifetime[0] Hosting environment: Development info: Microsoft.Hosting.Lifetime[0] Content root path: C:\GH\aspnet\docs\4\Docs\aspnetcore\tutorials\grpc\grpc-start\sample\GrpcGreeter info: Microsoft.AspNetCore.Hosting.Diagnostics[1] Request starting HTTP/2 POST https://localhost:5001/Greet.Greeter/SayHello application/grpc info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0] Executing endpoint 'gRPC - /Greet.Greeter/SayHello' info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1] Executed endpoint 'gRPC - /Greet.Greeter/SayHello' info: Microsoft.AspNetCore.Hosting.Diagnostics[2] Request finished in 78.32260000000001ms 200 application/grpc

备注

本文中的代码需要 ASP.NET Core HTTPS 开发证书来保护 gRPC 服务。 如果 .NET gRPC 客户端失败并显示消息 The remote certificate is invalid according to the validation procedure. 或 The SSL connection could not be established.,则开发证书不受信任。 要解决此问题,请参阅使用不受信任/无效的证书调用 gRPC 服务。

,