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

VS2019中.NET5 Azure函数运行报gRPC URI 'http://0'解析错误如何解决

异常原因
  1. 代码中手动注册了services.AddSingleton<IServiceProvider, ServiceProvider>();,这行代码会替换Azure Functions Worker运行时默认内置的服务提供程序,导致运行时依赖的内置环境变量配置(比如用于和Azure Functions主机通信的gRPC端口配置FUNCTIONS_WORKER_GRPC_PORT)无法被正确读取,最终解析出非法的gRPC地址http://0。
  2. 直接自行构建并注册IConfiguration单例的逻辑会覆盖Azure Functions运行时默认添加的内置配置源,也会导致运行时必要的配置项丢失。
解决方法
  • 首先删除手动注册IServiceProvider的代码行,不要修改运行时默认的服务提供程序实现。
  • 调整自定义配置的添加逻辑,使用官方提供的ConfigureAppConfiguration扩展方法添加自定义配置源,不会覆盖运行时内置的配置项。
  • 修正后的Program.cs参考代码如下:
public static void Main()
{
    var host = new HostBuilder()
            .ConfigureFunctionsWorkerDefaults()
            .ConfigureAppConfiguration((context, config) =>
            {
                var env = context.HostingEnvironment;
                config.SetBasePath(Directory.GetCurrentDirectory())
                      .AddJsonFile("AppSettings.json", false, true)
                      .AddJsonFile($"AppSettings.{env.EnvironmentName ?? "Production"}.json", true)
                      .AddEnvironmentVariables();
            })
            .ConfigureServices(services =>
            {
                // 在此处添加自定义的服务注册逻辑
            })
            .UseDefaultServiceProvider(options => options.ValidateScopes = false)
            .Build();

    host.Run();
}
  • 额外校验项:
    • 确认项目已安装适配.NET 5隔离进程模型的Microsoft.Azure.Functions.Worker系列NuGet包,且版本兼容。
    • 确认本地调试用的local.settings.json文件存在,且配置项FUNCTIONS_WORKER_RUNTIME的值为dotnet-isolated。

内容的提问来源于stack exchange,提问作者Yeasin Abedin

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 22:27:05