发布时间:2026-07-25阅读(1)
在本文中,你将了解



日志显示该服务正在侦听 HTTPS://localhost:5001。
控制台
info: Microsoft.Hosting.Lifetime[0] Now listening on: https://localhost:5001info: 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 项目文件:
gRPC 客户端项目需要以下包:
通过包管理器控制台 (PMC) 或管理 NuGet 包来安装包。
用于安装包的 PMC 选项XML
<ItemGroup> <Protobuf Include="Protos\greet.proto" GrpcServices="Client" /></ItemGroup>
构建客户端项目,以在 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 服务在写入命令提示符的日志中记录成功调用的详细信息:
控制台
info: Microsoft.Hosting.Lifetime[0] Now listening on: https://localhost:5001info: Microsoft.Hosting.Lifetime[0] Application started. Press Ctrl C to shut down.info: Microsoft.Hosting.Lifetime[0] Hosting environment: Developmentinfo: Microsoft.Hosting.Lifetime[0] Content root path: C:\GH\aspnet\docs\4\Docs\aspnetcore\tutorials\grpc\grpc-start\sample\GrpcGreeterinfo: Microsoft.AspNetCore.Hosting.Diagnostics[1] Request starting HTTP/2 POST https://localhost:5001/Greet.Greeter/SayHello application/grpcinfo: Microsoft.AspNetCore.Routing.EndpointMiddleware[0] Executing endpoint gRPC - /Greet.Greeter/SayHelloinfo: Microsoft.AspNetCore.Routing.EndpointMiddleware[1] Executed endpoint gRPC - /Greet.Greeter/SayHelloinfo: 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 服务。
Copyright © 2024 有趣生活 All Rights Reserve吉ICP备19000289号-5 TXT地图HTML地图XML地图