使用HTTP Debugger时调用AccessSecretVersion出现gRPC异常求助
解决HTTP Debugger下Google Secret Manager gRPC HTTP/2连接异常问题
问题场景
使用HTTP Debugger工具时,Google Cloud Secret Manager的AccessSecretVersion调用会抛出Grpc.Core.RpcException,提示无法建立HTTP/2连接;不使用该工具时代码运行正常。临时方案是在调用后启动调试器,但操作繁琐,需要彻底解决。
异常信息
Grpc.Core.RpcException HResult=0x80131500 Message=Status(StatusCode="Internal", Detail="Error starting gRPC call. HttpRequestException: Requesting HTTP version 2.0 with version policy RequestVersionOrHigher while unable to establish HTTP/2 connection.", DebugException="System.Net.Http.HttpRequestException: Requesting HTTP version 2.0 with version policy RequestVersionOrHigher while unable to establish HTTP/2 connection.") Source=Grpc.Net.Client StackTrace: at Grpc.Net.Client.Internal.GrpcCall`2.<GetResponseHeadersCoreAsync>d__72.MoveNext() at Grpc.Net.Client.Internal.HttpClientCallInvoker.BlockingUnaryCall[TRequest,TResponse](Method`2 method, String host, CallOptions options, TRequest request) at Grpc.Core.Interceptors.InterceptingCallInvoker.<BlockingUnaryCall>b__3_0[TRequest,TResponse](TRequest req, ClientInterceptorContext`2 ctx) at Grpc.Core.ClientBase.ClientBaseConfiguration.ClientBaseConfigurationInterceptor.BlockingUnaryCall[TRequest,TResponse](TRequest request, ClientInterceptorContext`2 context, BlockingUnaryCallContinuation`2 continuation) at Grpc.Core.Interceptors.InterceptingCallInvoker.BlockingUnaryCall[TRequest,TResponse](Method`2 method, String host, CallOptions options, TRequest request) at Google.Cloud.SecretManager.V1.SecretManagerService.SecretManagerServiceClient.AccessSecretVersion(AccessSecretVersionRequest request, CallOptions options) at Google.Api.Gax.Grpc.ApiCall.GrpcCallAdapter`2.CallSync(TRequest request, CallSettings callSettings) at Google.Api.Gax.Grpc.ApiCallRetryExtensions.<>c__DisplayClass1_0`2.<WithRetry>b__0(TRequest request, CallSettings callSettings) at .... This exception was originally thrown at this call stack: [External Code] Inner Exception 1: HttpRequestException: Requesting HTTP version 2.0 with version policy RequestVersionOrHigher while unable to establish HTTP/2 connection.
问题根源
gRPC默认使用HTTP/2协议,但HTTP Debugger工具可能不支持或无法正确拦截HTTP/2流量,导致客户端无法建立有效连接,进而触发异常。解决思路是强制gRPC客户端使用HTTP/1.1协议。
具体解决方案
通过自定义HttpClient并配置HTTP版本为1.1,再将其传入SecretManagerServiceClient的构建器,替代默认的客户端创建逻辑。修改后的完整代码如下:
public class SecretManagerService { private readonly SecretManagerServiceClient _client; private readonly string _projectId; private readonly string _secretName; public SecretManagerService(string projectId, string secretName) { // 自定义HttpClientHandler,配置代理并适配调试工具 var httpHandler = new HttpClientHandler { Proxy = new WebProxy(), UseProxy = true }; // 创建强制使用HTTP/1.1的HttpClient var httpClient = new HttpClient(httpHandler) { DefaultRequestVersion = HttpVersion.Version11, DefaultVersionPolicy = HttpVersionPolicy.RequestVersionExact }; // 通过构建器注入自定义HttpClient,创建SecretManager客户端 _client = new SecretManagerServiceClientBuilder { HttpClient = httpClient }.Build(); _projectId = projectId; _secretName = secretName; } public string GetSecretValue() { var secretVersionName = new SecretVersionName(_projectId, _secretName, "latest"); var secret = _client.AccessSecretVersion(secretVersionName); return secret.Payload.Data.ToStringUtf8(); } public void UpdateSecretValue(string newValue) { var secretName = new SecretName(_projectId, _secretName); var updatedPayload = new SecretPayload { Data = Google.Protobuf.ByteString.CopyFromUtf8(newValue) }; try { _client.AddSecretVersion(secretName, updatedPayload); } catch { Console.WriteLine(" Error updating secret"); } } }
关键修改点说明
- 替代全局代理设置:将原
HttpClient.DefaultProxy的全局配置移至自定义HttpClientHandler内部,避免影响其他HttpClient实例,同时保证代理配置生效。 - 强制HTTP/1.1:通过
DefaultRequestVersion指定使用HTTP/1.1,DefaultVersionPolicy设为RequestVersionExact确保客户端不会尝试升级到HTTP/2。 - 客户端构建器注入:使用
SecretManagerServiceClientBuilder替代默认的Create()方法,将自定义HttpClient传入,让gRPC调用完全遵循指定的HTTP协议规则。
验证
修改后启动HTTP Debugger工具,再次调用GetSecretValue方法,应能正常获取Secret值,不再抛出HTTP/2连接相关异常。
内容的提问来源于stack exchange,提问作者Hneel
相关产品推荐
相关产品推荐

