gpt4 book ai didi

c# - 具有未加密 HTTP2 连接的 .Net Core 3.1 gRPC 客户端

转载 作者:行者123 更新时间:2023-12-04 17:18:45 25 4
gpt4 key购买 nike

我正在尝试查询一些在 HTTP 上运行的本地 gRPC 服务。这是我的网络核心 3.1 控制台应用程序:

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" });
}
但它抛出了这个错误:

An exception of type 'Grpc.Core.RpcException' occurred inSystem.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 theconnection. HTTP/2 error code 'PROTOCOL_ERROR' (0x1).",DebugException="System.Net.Http.HttpRequestException: An erroroccurred while sending the request. --->System.Net.Http.Http2ConnectionException: The HTTP/2 server sentinvalid data on the connection. HTTP/2 error code 'PROTOCOL_ERROR'(0x1).


不幸的是,目前我们无法添加 SSL 证书,我们需要使用纯 HTTP 一段时间(服务应用程序尚未投入生产)。有没有人知道这里发生了什么?
根据 documentation AppContext.SetSwitch打电话应该就够了。
该服务器是一个 Asp Net Core 3.1 应用程序。它正在运行:
  • 端口 http://*:16050
  • 端口 https://*:16150。 这正确回复了客户端 但如上所述,目前只能在本地使用

  • 服务器提供
  • Http1 和 Http2
  • 同一端口上的 REST API 和 gRPC API

  • 客户正在使用以下软件包
      <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”表明:它将适用),但是:我已经看到非 TLS 端点存在此问题,该端点试图同时提供 HTTP/1 和 HTTP/2 流量 - 它有点困惑,并尝试使用 HTTP/1 响应对 HTTP/2 请求进行响应(这不会发生在 TLS 端点,因为握手的工作方式不同)。就我而言,修复方法是告诉服务器仅提供 HTTP/2 服务。这可以在 Kestrel 下的 appSettings.json 中完成。/EndPoints节点,通过指定 "Protocols": "Http2" ,或在 Program.cs 中指定 listenOptions.Protocols = HttpProtocols.Http2ConfigureKestrel称呼。当我想在没有 TLS 的情况下提供 HTTP/1 和 HTTP/2 服务时,我总是不得不使用不同的端口等。

    关于c# - 具有未加密 HTTP2 连接的 .Net Core 3.1 gRPC 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67521993/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com