gRPC客户端->服务器连接失败-服务器返回了无效或无法识别的响应

时间:2019-06-17 09:30:59

标签: c# asp.net-core grpc

我已经将MS described here的gRPC对(客户端+ Greeter服务)构建了起来

并尝试运行。我已经收到

服务器日志:

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添加了防火墙规则,但同样是相同的错误。 如何解决错误? 我做错了什么?

1 个答案:

答案 0 :(得分:0)

  1. ASPNET.Core文档站点包含错误: 我的客户端应用程序+来自github的gRPC服务器应用程序可以正常工作。 原因在硬编码的URL和客户端的Program.cs中:

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

vs

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文件,并确保它包含正确的数据。