升级至.NET 8 P6后gRPC调用失败问题求助
.NET 8 Preview 6升级后GrpcProtocolConstants类型初始化失败问题解析
错误信息
crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer (100) Unhandled Exception rendering component: Typeinitialization_Type,Grpc.Net.Client.Internal.GrpcFrotocolConstants System.TypeInitializationException: Typeinitialization_Type, grpc.net.client.internal.GrpcProtocolconstants system.FormatException: net_http_headers_invalid_value, application/grpc Exception_EndOfInnerExceptionstack at Microsoft.Extensions.Dependencyinjection.ServiceLookup.CallSiteVisitor'2 Microsoft.Extensions.Dependencyinjection.ServiceLookup.RuntimeResolver Context, Microsoft.Extensions.Dependencyznjection, version=8.0.0.0, Culture-neutral, PublickeyToken=adb9793829ddae60],[System.Object, System.Private.corelib, version:8.0.0.0" culture-neutral, PublickeyToken=adb9793829ddae60].VisitCaliSiteMain(ServiceCaliSite, RuntimeResolverContext) at Microsort.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeRe solver.VisitRootCache(ServiceCaliSite, RuntimeResolverContext) at Microsort.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor'2 ((Microsort.Extensions.DependencyInjection.ServiceLookup.RuntimeResolver Context, Microsort.Extensions.DependencyInjection, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[System.Object, system.Private.corelib, Version-8.0.0.0, Culture=neutral, PublickeyToken=7cec85d7bea7798e]].VisitCallSite(ServiceCallSite callSite, RuntimeResolverContext argument)
问题原因
- 核心触发点是
Grpc.Net.Client.Internal.GrpcProtocolConstants类型初始化失败,内部抛出FormatException,提示application/grpc这个HTTP头值无效。 - 这是.NET 8 Preview 6版本中Grpc.Net.Client组件的已知预览版bug:静态构造函数在初始化HTTP头常量时,新增的头格式验证逻辑出现兼容性问题,尤其在WebAssembly环境下部分设备会触发。
- 部分设备出现问题的差异,源于不同运行时环境(如浏览器内核)对HTTP头的解析规则执行严格程度不同。
解决建议
- 升级版本:直接升级到.NET 8正式版或更高版本的预览包,微软已在后续版本修复该类型初始化bug。
- 临时降级:若无法立即升级,可降级至.NET 8 Preview 5或之前的稳定版本,避开这个问题。
- 检查自定义配置:排查项目中是否有自定义修改Grpc相关HTTP头的代码,暂时移除自定义配置,恢复默认设置。
内容的提问来源于stack exchange,提问作者Smith5727
相关产品推荐
相关产品推荐

