azure-functions.net-5

System.InvalidOperationException: The gRPC channel URI 'http://0' could not be parsed


Using .NET5 Azure function in Visual Studio 2019, I am getting the below exception from Program.cs:

System.InvalidOperationException: The gRPC channel URI 'http://0' could not be parsed

My Program.cs is below:

public static void Main()
{
    var host = new HostBuilder()
            .ConfigureFunctionsWorkerDefaults()
            .ConfigureServices(services =>
            {
                services.AddSingleton<IConfiguration>(data =>
                {
                    var result = new ConfigurationBuilder()
                        .SetBasePath(Directory.GetCurrentDirectory())
                        .AddJsonFile("AppSettings.json", false, true)
                        .AddJsonFile($"AppSettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", true)
                        .AddEnvironmentVariables()
                        .Build();
                    return result;
                });

                services.AddSingleton<IServiceProvider, ServiceProvider>();
            })
            .UseDefaultServiceProvider(options => options.ValidateScopes = false)
            .Build();

    host.Run();
}

The exception is being thrown from host.Run() in Debug mode. Any clue?


Solution

  • My issue has been solved. Once I set the IConfiguration from ConfigureAppConfiguratio middleware, the exception is gone

    public static void Main()
    {
        var host = new HostBuilder()
                        .ConfigureFunctionsWorkerDefaults()
                        .ConfigureAppConfiguration(config =>
                        {
                            config.SetBasePath(Directory.GetCurrentDirectory())
                                .AddJsonFile("AppSettings.json", false, true)
                                .AddJsonFile(
                                    $"AppSettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json",
                                    true)
                                .AddEnvironmentVariables();
                        })
                        .ConfigureServices(services =>
                        {
                            
                        })
                        .UseDefaultServiceProvider(options => options.ValidateScopes = false)
                        .Build();
    
                host.Run();
    }