zoukankan      html  css  js  c++  java
  • .net core中Grpc使用报错:The response ended prematurely.

      当我们调用Grpc是出现下面的一堆异常时,一般是由于LTS导致的:  

      Call failed with gRPC error status. Status code: 'Unavailable', Message: 'Error starting gRPC call. HttpRequestException: An error occurred while sending the request. IOException: The response ended prematurely.'.
      fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
          An unhandled exception has occurred while executing the request.
      Grpc.Core.RpcException: Status(StatusCode="Unavailable", Detail="Error starting gRPC call. HttpRequestException: An error occurred while sending the request. IOException: The response ended prematurely.", DebugException="System.Net.Http.HttpRequestException: An error occurred while sending the request.
      ---> System.IO.IOException: The response ended prematurely.
      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.DiagnosticsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
      at Microsoft.Extensions.Http.Logging.LoggingHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
      at Microsoft.Extensions.Http.Logging.LoggingScopeHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
      at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
      at Grpc.Net.Client.Internal.GrpcCall`2.RunCall(HttpRequestMessage request, Nullable`1 timeout)")
      at Grpc.Net.Client.Internal.HttpClientCallInvoker.BlockingUnaryCall[TRequest,TResponse](Method`2 method, String host, CallOptions options, TRequest request)
      .........

      首先我们知道,Grpc是基于HTTP/2作为通信协议的,而HTTP/2默认是基于LTS/SSL加密技术的,或者说默认需要https协议支持(https=http+lts/ssl),而HTTP/2又支持明文传输,即对http也是支持,但是一般需要我们自己去设置。

      当我们使用Grpc时,又不去改变这个默认行为,那可能就会导致上面的报错。

      在.net core开发中,Grpc要支持http,我们需要显示的指定不需要LTS支持,官方给出的做法是添加如下配置(比如客户端(调用方在ConfigureServices添加):  

        public void ConfigureServices(IServiceCollection services)
        {
            //显式的指定HTTP/2不需要TLS支持
            AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true); 
            AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2Support", true);
    
            services.AddGrpcClient<Greeter.GreeterClient>(nameof(Greeter.GreeterClient), options =>
            {
                options.Address = new Uri("http://localhost:5000");
            });
    
            ...
        }
  • 相关阅读:
    毕业面试试题汇总
    js获取系统日期
    非常酷的3D翻转相册展示特效
    CSS 替换元素和非替换元素 行内非替换元素
    怎样在linux下编写C程序并编译执行
    库和框架的区别
    转载:em(倍)与px的区别
    RPMForge介绍及安装
    linux下安装jdk和配置环境变量
    PCI PCI-X PCI-E介绍
  • 原文地址:https://www.cnblogs.com/shanfeng1000/p/14150906.html
Copyright © 2011-2022 走看看