【问题标题】:gRPC Client->Server connection failed - The server returned an invalid or unrecognized responsegRPC 客户端-> 服务器连接失败 - 服务器返回无效或无法识别的响应
【发布时间】:2019-06-17 09:30:59
【问题描述】:

我已将 gRPC 对(客户端+Greeter 服务)构建为 MS described here

并试图运行。我收到了

服务器日志:

dbug: Grpc.AspNetCore.Server.Internal.GrpcServiceBinder[1]
Added gRPC method 'SayHello' to service 'Greet.Greeter'. Method type: 'Unary', route pattern: '/Greet.Greeter/SayHello'.
info: Microsoft.Hosting.Lifetime[0]
Now listening on: http://localhost:50051
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: E:\gRPC\GrpcGreeter\GrpcGreeter
info: Microsoft.AspNetCore.Server.Kestrel[29]
Connection id "0HLNJ0DILMG0A": HTTP/2 connection error.
Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http2.Http2ConnectionErrorException: HTTP/2 connection error (PROTOCOL_ERROR): Invalid HTTP/2 connection preface.
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http2.Http2Connection.ParsePreface(ReadOnlySequence1& buffer, SequencePosition& consumed, SequencePosition& examined) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http2.Http2Connection.TryReadPrefaceAsync() at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http2.Http2Connection.ProcessRequestsAsync[TContext](IHttpApplication1 application)

客户的日志

Unhandled Exception: System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.IO.IOException: The server returned an invalid or unrecognized response.
at System.Net.Http.HttpConnection.FillAsync()
at System.Net.Http.HttpConnection.ReadNextResponseHeaderLineAsync(Boolean foldedHeadersAllowed)
at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithNtConnectionAuthAsync(HttpConnection connection, HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts) at Grpc.Net.Client.Internal.GrpcCall2.SendAsync(HttpClient client, HttpRequestMessage message)
at Grpc.Net.Client.Internal.GrpcCall`2.GetResponseHeadersAsync()
E:\gRPC\GrpcGreeterClient\GrpcGreeterClient\bin\Debug\netcoreapp3.0\GrpcGreeterClient.exe (process 17980) exited with code -532462766.
Press any key to close this window . . .

我已经为 50051 添加了防火墙规则,但同样的错误再次出现。 如何修复错误? 我做错了什么?

【问题讨论】:

  • 它在 localhost:50051 上监听?它肯定需要侦听所有 IP 或至少要联系的网络的主 IP - 但看起来你要联系 - 你需要检查响应是什么......除了“错误”
  • 我找到了错误的原因:MS doc 包含错误,以及他们的 github 代码示例。再次:需要小心;-)

标签: c# asp.net-core grpc


【解决方案1】:
  1. ASPNET.Core 文档站点包含错误: 我的客户端应用程序 + 来自 github 的 gRPC 服务器应用程序有效。 原因在于硬编码的 URL 和客户端的 Program.cs:

    var channel = new Channel("localhost:50051", ChannelCredentials.Insecure);

var httpClient = new HttpClient();
// The port number(50051) must match the port of the gRPC server.
 httpClient.BaseAddress = new Uri("http://localhost:50051");
  1. 另外,请查看 launchSettings.json 文件并确保其中包含正确的数据。

【讨论】:

  • 我也有同样的问题。我知道端口匹配,因为我不得不为另一个问题更改它们。你在哪里使用var channel = new Channel("localhost:50051", ChannelCredentials.Insecure);
猜你喜欢
  • 2021-08-02
  • 1970-01-01
  • 2019-07-20
  • 1970-01-01
  • 2020-06-26
  • 1970-01-01
  • 1970-01-01
  • 2020-01-27
  • 1970-01-01
相关资源
最近更新 更多