【发布时间】:2020-01-27 05:20:28
【问题描述】:
无法从使用 grpc.core 库(Grpc.Core.2.24.0 和 Grpc.Core.Api.2.24.0)的 .net 框架应用程序编写的 greeter 客户端连接到此链接中提到的 greeter grpc 服务 - https://docs.microsoft.com/en-us/aspnet/core/tutorials/grpc/grpc-start?view=aspnetcore-3.0。
下面是我的客户端代码。它适用于非 SSL 但不适用于 SSL
非 SSL 的客户端代码(可行)
var channel = new Channel("localhost:5000", ChannelCredentials.Insecure);
var client = new Greeter.GreeterClient(channel);
var reply = await client.SayHelloAsync(new HelloRequest { Name = "GreeterClient" });
channel.ShutdownAsync().Wait();
带有 SSL 的客户端代码(连接失败)
SslCredentials secureChannel = new SslCredentials();
var channel = new Channel("localhost", 5001, secureChannel);
var client = new Greeter.GreeterClient(channel);
var reply = await client.SayHelloAsync(new HelloRequest { Name = "GreeterClient" });
channel.ShutdownAsync().Wait();
我在使用 SSL 时遇到的错误是:
Grpc.Core.RpcException: 'Status(StatusCode=Unavailable, Detail="failed to connect to all addresses")'
我尝试使用在同一个链接 (https://docs.microsoft.com/en-us/aspnet/core/tutorials/grpc/grpc-start?view=aspnetcore-3.0) 中提到的 .net 核心应用程序客户端,它适用于 SSL 和非 SSL,但不是直接使用 grp 库。我的客户端是一个 .Net 框架客户端,这就是我无法使用 .net 库连接到 grpc 服务的原因。 .Net grpc 库仅受 .net 核心应用支持。
SslCredentials secureChannel = new SslCredentials();
var channel = new Channel("localhost", 5001, secureChannel);
var client = new Greeter.GreeterClient(channel);
var reply = await client.SayHelloAsync(new HelloRequest { Name = "GreeterClient" });
channel.ShutdownAsync().Wait();
预期结果 - 来自服务器的响应
实际结果 - Grpc.Core.RpcException: 'Status(StatusCode=Unavailable, Detail="failed to connect to all addresses")'
【问题讨论】:
-
因为您的目标是“localhost”,所以我的第一个猜测是 SSL 安全名称检查失败。请注意,除非您覆盖频道上的“权限”(有一个频道选项可以执行此操作,但它实际上是为使用 localhost 进行测试而不是生产使用而设计的),您的客户端将尝试验证服务器的证书是否验证了您的名称频道正在定位 - 在本例中为“localhost”