具有未加密 HTTP2 连接的 .Net Core 3.1 gRPC 客户端 [英] .Net Core 3.1 gRPC client with unencrypted HTTP2 connection

查看:22
本文介绍了具有未加密 HTTP2 连接的 .Net Core 3.1 gRPC 客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查询一些在 HTTP 上运行的本地 gRPC 服务.这是我的 net core 3.1 控制台应用程序:

I am trying to query some local gRPC services running on HTTP. This is my net core 3.1 console app:

static async Task Main(string[] args)
{
    AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);

    using var channel = GrpcChannel.ForAddress("http://localhost:16050");
    var client = new EventService.EventServiceClient(channel);
    var reply1 = await client.GetTrackEventAsync(new GetTrackEventRequest { Name = "01Austra14" });
}

但它抛出了这个错误:

'Grpc.Core.RpcException' 类型的异常发生在System.Private.CoreLib.dll 但未在用户代码中处理:'Status(StatusCode=Internal",Detail=启动 gRPC 调用时出错.HttpRequestException: 发送请求时出错.Http2ConnectionException: HTTP/2 服务器在联系.HTTP/2 错误代码PROTOCOL_ERROR"(0x1).",DebugException="System.Net.Http.HttpRequestException: 一个错误发送请求时发生.--->System.Net.Http.Http2ConnectionException: HTTP/2 服务器发送连接上的无效数据.HTTP/2 错误代码PROTOCOL_ERROR"(0x1).

An exception of type 'Grpc.Core.RpcException' occurred in System.Private.CoreLib.dll but was not handled in user code: 'Status(StatusCode="Internal", Detail="Error starting gRPC call. HttpRequestException: An error occurred while sending the request. Http2ConnectionException: The HTTP/2 server sent invalid data on the connection. HTTP/2 error code 'PROTOCOL_ERROR' (0x1).", DebugException="System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.Http.Http2ConnectionException: The HTTP/2 server sent invalid data on the connection. HTTP/2 error code 'PROTOCOL_ERROR' (0x1).

不幸的是,目前我们无法添加 SSL 证书,我们需要使用纯 HTTP 一段时间(服务应用程序尚未投入生产).有人知道这里发生了什么吗?

Unfortunately at the moment we can't add an SSL certificate and we need to use plain HTTP for a while (the service app is not in production yet). Does anyone have an idea on what is happening here?

根据 文档 AppContext.SetSwitch 调用应该足够了.

According to the documentation the AppContext.SetSwitch call should be enough.

服务器是一个 Asp Net Core 3.1 应用程序.它正在运行:

The server is an Asp Net Core 3.1 app. It is running on:

  • 端口 http://*:16050
  • 端口 https://*:16150.这会正确回复客户端,但如上所述,目前只能在本地使用
  • port http://*:16050
  • port https://*:16150. This replies correctly to the client but as explained above, can be used only locally at the moment

服务器提供

  • Http1 和 Http2
  • 同一端口上的 REST API 和 gRPC API

客户正在使用以下软件包

Client is using the following packages

  <ItemGroup>
    <PackageReference Include="Google.Protobuf" Version="3.17.0" />
    <PackageReference Include="Grpc.Net.Client" Version="2.37.0" />
    <PackageReference Include="Grpc.Tools" Version="2.37.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
  </ItemGroup>

推荐答案

我不能 100% 确定这适用于您的场景(尽管Http1 和 Http2"建议:它将适用),但是:我已经在尝试同时提供 HTTP/1 和 HTTP/2 流量的非 TLS 端点上看到了这个问题 - 它有点困惑并试图用 HTTP/1 响应来响应 HTTP/2 请求(这不会TLS 端点会发生这种情况,因为握手的工作方式不同).就我而言,解决方法是告诉服务器提供 HTTP/2 服务.这可以在 Kestrel/EndPoints 节点下的 appSettings.json 中完成,通过指定 Protocols": Http2",或在Program.cs 通过在 ConfigureKestrel 调用中指定 listenOptions.Protocols = HttpProtocols.Http2.当我想在没有 TLS 的情况下提供 HTTP/1 和 HTTP/2 服务时,我总是不得不使用不同的端口等.

I can't be 100% sure this applies in your scenario (although the "Http1 and Http2" suggests: it will apply), but: I've seen this problem with a non-TLS endpoint that is attempting to serve both HTTP/1 and HTTP/2 traffic - it gets a little confused and tries to respond with HTTP/1 response to an HTTP/2 request (this doesn't happen with a TLS endpoint, due to the handshake working differently). In my case, the fix was to tell the server to only serve HTTP/2. This can be done in appSettings.json under the Kestrel / EndPoints node, by specifying "Protocols": "Http2", or in Program.cs by specifying listenOptions.Protocols = HttpProtocols.Http2 in the ConfigureKestrel call. When I want to serve HTTP/1 and HTTP/2 without TLS, I've always had to use different ports etc.

这篇关于具有未加密 HTTP2 连接的 .Net Core 3.1 gRPC 客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆