You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 8部署Azure Web Service的gRPC调用失败配置排查

问题:Azure部署的gRPC服务无法正常工作

感谢JNK等人在ASP.NET Core文档库中提供的详尽文档与示例。我已接近目标,但仍无法为部署在Azure上的gRPC启用型Web服务搭建稳定的开发与运维环境。

我上一次接触ASP.NET Core gRPC是在2021年,当时因找不到合适的配置组合来搭建可用于DevOps的、托管在Azure Linux容器中的ASP.NET Core Web服务而搁置了项目——那时Azure的Linux Docker应用服务还较新。

我认为关键难点始终在于:

  • 针对开发与部署场景,结合Azure托管SSL证书与服务的正确Kestrel设置
  • Azure中相关的SSL与HTTP2.0配置

当前错误

我基于.NET 8 Linux容器、使用ASP.NET Core gRPC-Web开发的代码已部署到Azure,ASP.NET Core页面功能、SSL(Azure证书与KeyVault密钥存储)、身份验证及SQL连接等均正常,但调用Web服务中的gRPC服务时出现以下错误:

Grpc.Core.RpcException
HResult=0x80131500
Message=Status(StatusCode="Internal", Detail="Error starting gRPC call. HttpRequestException: The HTTP/2 server sent invalid data on the connection. HTTP/2 error code 'PROTOCOL_ERROR' (0x1). (HttpProtocolError) HttpProtocolException: The HTTP/2 server sent invalid data on the connection. HTTP/2 error code 'PROTOCOL_ERROR' (0x1). (HttpProtocolError)", DebugException="System.Net.Http.HttpRequestException: The HTTP/2 server sent invalid data on the connection. HTTP/2 error code 'PROTOCOL_ERROR' (0x1). (HttpProtocolError)")

相关Azure应用服务运行时设置

  • HTTP: 2.0
  • HTTP 2.0 Proxy: 开启*
  • 最低入站TLS: 1.2
  • HTTP20_ONLY_PORT: 8585

*尝试将其设置为仅gRPC会导致页面无法正常服务,我仍需提供页面服务。(ASP.NET Core上gRPC-Web的正确设置是什么?)
**曾参考官方文档添加此环境设置以支持Azure上的gRPC-Web

当前appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Trace",
      "System": "Trace",
      "Grpc": "Trace",
      "Microsoft": "Trace",
      "Microsoft.AspNetCore": "Trace"
    }
  },
  "AllowedHosts": "*",
  "Kestrel": {
    "EndpointDefaults": {
      "Protocols": "Http2"
    }
  }
}

相关代码片段

Program.cs中的ConfigureHttps方法

private static async void ConfigureHttps(HttpsConnectionAdapterOptions options)
{
    try
    {
            // 设置Kestrel HTTPS证书
            options.ServerCertificate = await SetRuntimeCertificate(false);

            if (options.ServerCertificate == null) 
            {
                throw new Exception("SSL证书无法获取。");
            }

            //TODO: 确定使用TLS13还是TLS12
            options.SslProtocols = System.Security.Authentication.SslProtocols.Tls13;
                
    }
    catch (Exception ex)
    {
        Console.Error.WriteLine($"加载HTTPS证书失败: {ex}");
        throw;
    }
}

Startup.cs中的ConfigureServices方法片段

services.AddGrpc(o => o.EnableDetailedErrors = true);           

Startup.cs中的Configure方法片段

if (env.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
    app.UseMigrationsEndPoint();
}
else
{
    app.UseExceptionHandler("/Error");
    // 默认HSTS值为30天。生产场景可能需要调整,详见官方说明。
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();

app.UseRouting();

// 启用gRPC-Web
app.UseGrpcWeb(new GrpcWebOptions { DefaultEnabled = true });

app.UseIdentityServer();
app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
    // gRPC端点需通过gRPC客户端调用。
    endpoints.MapGrpcService<MembershipService>().EnableGrpcWeb();
    endpoints.MapRazorPages();
});

寻求帮助

是否有人能提供示例,或修改ASP.NET Docs中的gRPC示例,演示搭建ASP.NET Core Web服务的正确方式:

  • 面向Azure Docker Linux(支持HTTP2)
  • 包含在Docker开发环境中使用SSL进行开发与测试所需的配置——Linux Docker Azure部署已是标准方式,我认为这是在Azure Web服务上实现完整gRPC的唯一方式

我需要一个示例解决方案,展示如何在本地可靠开发测试,同时定期发布到Azure。

再次求助:是否有可靠的示例或文档展示如何将支持gRPC的ASP.NET Core应用部署为Azure Web服务?或者有人能发现我上述代码初始化方式中的问题并提供修复方案?

内容的提问来源于stack exchange,提问作者David Gerding

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 18:45:53