.NET 5 GRPC 客户端调用引发异常:在未启用 HTTP/2 时请求具有版本策略 RequestVersionOrHigher 的 HTTP 版本 2.0 [英] .NET 5 GRPC client call throws exception: Requesting HTTP version 2.0 with version policy RequestVersionOrHigher while HTTP/2 is not enabled

查看:293
本文介绍了.NET 5 GRPC 客户端调用引发异常:在未启用 HTTP/2 时请求具有版本策略 RequestVersionOrHigher 的 HTTP 版本 2.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一个 gRPC 应用程序.我正在尝试从 .NET 5 gRPC 客户端 (Grpc.Net.Client 2.35.0) 调用服务器流式 RPC 调用,这在我的本地开发环境中导致以下异常:

This is my first gRPC application. I'm attempting to invoke a server-streaming RPC call from a .NET 5 gRPC client (Grpc.Net.Client 2.35.0) which results in the following exception in my local development environment:

Grpc.Core.RpcException:状态(状态代码=内部",细节=错误"启动 gRPC 调用.HttpRequestException:请求 HTTP 2.0 版使用版本策略 RequestVersionOrHigher 而不是 HTTP/2启用."

Grpc.Core.RpcException: Status(StatusCode="Internal", Detail="Error starting gRPC call. HttpRequestException: Requesting HTTP version 2.0 with version policy RequestVersionOrHigher while HTTP/2 is not enabled."

无论服务器应用程序是否正在运行,都会发生这种情况.这是我用来拨打电话的代码:

This occurs whether or not the server application is running. Here is the code I'm using to make the call:

using var channel = GrpcChannel.ForAddress(@"http://localhost:5000");
var client = new AlgorithmRunner.AlgorithmRunnerClient(channel);
using var call = client.RunAlgorithm(new RunAlgorithmRequest());

while (await call.ResponseStream.MoveNext())
{

}

我的理解是 .NET 5 gRPC 应该默认启用 HTTP/2:为什么异常表示未启用 HTTP/2,我该如何解决?

My understanding is that .NET 5 gRPC is supposed to have HTTP/2 enabled by default: why does the exception indicate that HTTP/2 is not enabled and how do I resolve it?

推荐答案

我在公司代理后面遇到同样的问题并收到此错误消息

I faced the same problem sitting behind a corporate proxy and receiving this error message

An exception of type 'Grpc.Core.RpcException' occurred in System.Private.CoreLib.dll but was not handled in user code: 'Status(StatusCode="Unavailable", Detail="Error starting gRPC call. HttpRequestException: Connection refused SocketException: Connection refused", DebugException="System.Net.Http.HttpRequestException: Connection refused

我完全同意您建议的通过覆盖 DefaultProxy 绕过所有代理设置的解决方法是合法且有效的.

I fully agree that your suggested workaround with bypassing all proxy settings by overwriting the DefaultProxy is legit and functional.

更好的方法是不要在代码中硬编码绕过所有代理语句",而是使用 no_proxy 环境变量.这个环境变量的目的是定义一个规则来排除去往某些主机的流量

A slightly better approach would be not to hard code a 'bypass all proxy statement' in your code and use instead the no_proxy environment variable. The purpose of this environment variable is to define a rule for excluding traffic destined to certain hosts

解决方案

激活环境.变量 no_proxy 用于您的开发设置,例如

Activate the env. variable no_proxy for your development setup like

对于Linux

export no_proxy=localhost,127.0.0.1

对于 Dockerfile

For Dockerfile

ENV no_proxy=localhost,127.0.0.1

关于代理环境变量

当你启动你的 gRPC 客户端应用程序时,检查环境变量中读取的值是很重要的,因为 gRPC 在引擎盖下使用了 HttpClient 类,它考虑了所有代理环境变量.如果未设置 no_proxy 值,则无效.

It is important when you start your gRPC client application, that you check what the value of the read in environment variables is, because gRPC uses the HttpClient class under the hood, which considers all proxy environment variables. If the no_proxy value is not set, it has not effect.

var noProxy = Environment.GetEnvironmentVariable("no_proxy");

这篇关于.NET 5 GRPC 客户端调用引发异常:在未启用 HTTP/2 时请求具有版本策略 RequestVersionOrHigher 的 HTTP 版本 2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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