gpt4 book ai didi

c# - GRPC - HTTP/2 服务器在连接上发送了无效数据。从 .netcore 3.1 升级到 net5 或 net6 时的 HTTP/2 错误代码 'PROTOCOL_ERROR

转载 作者:行者123 更新时间:2023-12-05 09:01:41 27 4
gpt4 key购买 nike

我有一个调用 GRPC 服务的 .netcore3.1 测试项目。
该服务只能通过 HTTP 获得。我现在无法更改服务。

当我将项目更新为 .net5-windows 或 .net6-windows(实际上只是 csproj 更改)时,调用失败并出现以下错误:

Grpc.Core.RpcException : Status(StatusCode="Unavailable", Detail="Error starting gRPC call. HttpRequestException: An error occurred while sending the request. IOException: The request was aborted. 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.IO.IOException: The request was aborted.---> System.Net.Http.Http2ConnectionException: The HTTP/2 server sent invalid data on the connection. HTTP/2 error code 'PROTOCOL_ERROR' (0x1).at System.Net.Http.Http2Connection.ThrowProtocolError(Http2ProtocolErrorCode errorCode)at System.Net.Http.Http2Connection.ReadFrameAsync(Boolean initialFrame)at System.Net.Http.Http2Connection.ProcessIncomingFramesAsync()--- End of inner exception stack trace ---at System.Net.Http.Http2Connection.ThrowRequestAborted(Exception innerException)at System.Net.Http.Http2Connection.Http2Stream.CheckResponseBodyState()at System.Net.Http.Http2Connection.Http2Stream.TryEnsureHeaders()at System.Net.Http.Http2Connection.Http2Stream.ReadResponseHeadersAsync(CancellationToken cancellationToken)at System.Net.Http.Http2Connection.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)--- End of inner exception stack trace ---at System.Net.Http.Http2Connection.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)at System.Net.Http.HttpConnectionPool.SendWithVersionDetectionAndRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken)at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)at Grpc.Net.Client.Web.GrpcWebHandler.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)at System.Net.Http.HttpClient.g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)at Grpc.Net.Client.Internal.GrpcCall2.RunCall(HttpRequestMessage request, Nullable1 timeout)")at..

我查看了一些 SO 问题(例如 .Net Core 3.1 gRPC client with unencrypted HTTP2 connectionGrpc.Core.RpcException: 'Status (StatusCode = "Unavailable", Detail = "Error starting gRPC call)以及“调用不安全服务”故障排除指南 https://learn.microsoft.com/en-us/aspnet/core/grpc/troubleshoot?view=aspnetcore-5.0#call-insecure-grpc-services-with-net-core-client但是它们没有帮助。

在初始化客户端之前,我尝试了一些像这样设置 AppContext 的选项(即使它们应该申请 netcore3.1),但同样没有帮助:

  AppContext.SetSwitch(“System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport”, true);
AppContext.SetSwitch(“System.Net.Http.SocketsHttpHandler.Http2Support”, true);

channel 初始化如下:

 var baseUri = new Uri(baseUrl);
HttpMessageHandler httpMessageHandler = new HttpClientHandler();
if (baseUri.AbsolutePath != "/")
httpMessageHandler = new SubDirectoryHandler(httpMessageHandler, baseUri.AbsolutePath);
var handler = new GrpcWebHandler(GrpcWebMode.GrpcWebText, httpMessageHandler);
_channel = GrpcChannel.ForAddress(baseUrl, new GrpcChannelOptions { HttpClient = new HttpClient(handler) });

在发送请求之前检查 GRPC channel 显示请求版本为 1.1(不考虑 AppContext 设置和 TargetFramework):

enter image description here

GRPC 客户端库是:

Grpc.Net.Client v. 2.42.0
Grpc.Net.Client.Web v. 2.42.0

发送请求时,服务端在输出中记录以下内容:

Exception thrown: 'Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException' in Microsoft.AspNetCore.Server.Kestrel.Core.dll
Exception thrown: 'Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException' in System.Private.CoreLib.dll'TradingOrchestrationService.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App\6.0.5\Microsoft.AspNetCore.WebUtilities.dll'.
Exception thrown: 'System.Net.Sockets.SocketException' in Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.dll
The I/O operation has been aborted because of either a thread exit or an application request.

我想强调的关键是工作行为和非工作行为之间的唯一区别是将入门程序集中的 TargetFramework 版本从 3.1 提高到 5 或 6。

最佳答案

为了完整性和 future 引用,我将在 GitHub 上发布从 JamesNK 得到的回复 https://github.com/dotnet/runtime/issues/71906

When using HTTP then the client and server must use the same protocol.Without https then there is no negotiation.

What protocol is Kestrel configured to use? HTTP/1.1 or HTTP/2? If youhaven't explicitly configured it to only HTTP/2 then it will probablybe HTTP/1.1.

GrpcWebHandler has a HttpVersion property on it that you can set toexplicitly specify what HTTP version you want. You could try settingit to new Version(1, 1).

设置此属性是否有任何其他影响?
当我们迁移服务器以使用 HTTP/2 时,我想它必须被删除?

Yes

What happened to you: in .NET Core 3.x the gRPC client was configuringthe gRPC request to be HTTP/2, but because you weren't using TLS therequest was silently downgraded to HTTP/1.1

That was fixed in .NET 5+, but you were relying on broken behavior sothe fix broke you.

The client is now sending HTTP/2 over http, but your server isconfigured for HTTP/1.1. That creates a protocol error.GrpcWebHandler.HttpVersion provides a way for you to override the HTTPversion and to specify exactly what you want (because gRPC-Web workswith both HTTP versions).

关于c# - GRPC - HTTP/2 服务器在连接上发送了无效数据。从 .netcore 3.1 升级到 net5 或 net6 时的 HTTP/2 错误代码 'PROTOCOL_ERROR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72927587/

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