10

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?

Kalle
  • 2,282
  • 1
  • 24
  • 30
Yeasin Abedin
  • 2,081
  • 4
  • 23
  • 41
  • 1
    Hello @Yeasin Abedin, Could you please try to run your function by using this command `func host start` in [Azure Functions Core Tools](https://github.com/Azure/azure-functions-dotnet-worker#install-the-azure-functions-core-tools) ,as mentioned in this SO Thread :https://stackoverflow.com/questions/67013422/upgrading-azure-functions-to-5-0-system-uriformatexception , And please let me know if it works or if the same issue still happening. – AjayKumarGhose Nov 22 '21 at 09:40
  • Please check my answer. I didn't have to use Azure Function Core Tools. – Yeasin Abedin Nov 23 '21 at 08:44

5 Answers5

25

For me it was occurring in Rider. The issue was that I was running the Function App as a .Net Project instead of as Azure Functions host.

Rider Edit Run/Debug Configuration

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
ubienewbie
  • 1,771
  • 17
  • 31
  • You need the plugin https://plugins.jetbrains.com/plugin/11220-azure-toolkit-for-rider – Michael Blake Aug 05 '22 at 08:58
  • How exactly did you run as Function App? Which steps did you follow? Could you please elaborate on that? – Serhat Jan 27 '23 at 12:38
  • @Serhat, I agree that this is the correct answer and that it's not descriptive enough, so I'll answer. Rider (three lines menu) -> Run -> Edit Configurations. You'll see items in the .net projects. Select them and click the minus. Select the Azure functions host. Click the plus. Select the project that is your function app. Give it a name. In my case the name was in conflict so I gave it a new name, but then had to close rider, delete the .idea folder and restart rider. Once I went back into the run configuration, I was able to rename the configuration correctly. – Dave Loukola Feb 23 '23 at 15:25
10

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();
}
Yeasin Abedin
  • 2,081
  • 4
  • 23
  • 41
5

In my case, shutting down Visual Studio 2022 and opening it in Administrator mode solved my issue

Pau 24
  • 93
  • 1
  • 4
3

I was having the same problem in VS 2022, .NET 7. Adding the nuget package for

Microsoft.Azure.Functions.Worker.Sdk

solved this for me.

Tom
  • 61
  • 1
  • 5
0

I had a similar issue and I found out that the problem was caused by the services.AddConfigurations method in the ConfigureServices extension method. This method was overriding the settings done by the ConfigureFunctionsWorkerDefaults() method. To fix this, I had to include the hostContext.Configuration in the AddConfigurations() method, like this:

var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.ConfigureServices((hostContext, services) =>
{
    var location = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);

    services.AddConfigurations(c => c.SetBasePath(location)
                        .AddJsonFile("example.settings.json", optional: false, reloadOnChange: true)
                        .AddConfiguration(hostContext.Configuration));
}).Build();
host.Run();

This way, the configuration from the hostContext is merged with the configuration from the JSON file, and the settings are not overridden. I hope this helps you solve your problem.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77