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

12

27

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?

Barretter answered 21/11, 2021 at 3:49 Comment(2)
Hello @Yeasin Abedin, Could you please try to run your function by using this command func host start in Azure Functions Core Tools ,as mentioned in this SO Thread :#67013922 , And please let me know if it works or if the same issue still happening.Apocalyptic
Please check my answer. I didn't have to use Azure Function Core Tools.Barretter
B
12

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();
}
Barretter answered 23/11, 2021 at 8:42 Comment(3)
error CS0106: The modifier 'public' is not valid for this item [/private/tmp/foo/LocalFunctionProj/LocalFunctionProj.csproj]Casandracasanova
Came accross this when upgrading from .NET 6 to .NET 7. This was 100% the issue.Intumescence
unfortunately it did not workEmpery
M
62

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

Masterpiece answered 25/5, 2022 at 4:43 Comment(4)
You need the plugin plugins.jetbrains.com/plugin/11220-azure-toolkit-for-riderBloomer
How exactly did you run as Function App? Which steps did you follow? Could you please elaborate on that?Atalee
@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.Gratian
Similar problem, However what caused it was the fact that I had launched Rider Preview where azure plugin was not compatibleNaturalize
B
12

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();
}
Barretter answered 23/11, 2021 at 8:42 Comment(3)
error CS0106: The modifier 'public' is not valid for this item [/private/tmp/foo/LocalFunctionProj/LocalFunctionProj.csproj]Casandracasanova
Came accross this when upgrading from .NET 6 to .NET 7. This was 100% the issue.Intumescence
unfortunately it did not workEmpery
C
12

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.

Curtsy answered 12/1, 2023 at 14:20 Comment(2)
Solved my issue as well with a function I was migration from .Net 6 to .Net 7.Biparietal
When moving from .NET7.0 STS to .NET.8.0 LTSZombie
M
5

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

Melesa answered 25/11, 2022 at 12:36 Comment(1)
It did not resolve the issue in my case.Mellins
B
2

Use func start instead of dotnet run in your command line. Im using VS Code and it worked for me.

Burnette answered 18/12, 2023 at 23:34 Comment(1)
unfortunately it did not workEmpery
S
1

I had this issue, in my case I had 2 installations of visual studio, one of them didn't have the "Azure development" workload included, and I was trying to run the Function app using that installation.

Sligo answered 18/2 at 22:2 Comment(1)
For me it happened even when I had only one installation without Azure workload. Thanks it worked.Debate
B
1

I had this error in Visual Studio 2022 after installing .NET8, F5 debug would no longer open into the local Azure Functions runtime.

To get it working again, I had to go to Tools -> Options -> Projects & Solutions -> Azure functions. Then check for updates and install updates, and restart Visual Studio.

This is documented in a GitHub issue here.

Beirut answered 1/3 at 23:44 Comment(1)
unfortunately it did not workEmpery
D
1

I have azure function project which was running fine with func start command but I was not able to debug. After spending a day, I found the solution which is very basic solution which saved my day.

1) Open .vscode folder and add launch.json file if it does not exists, and add this to launch.json file.

  {
  "version": "0.2.0",
  "configurations": [
    {
      "name": ".NET Core Attach",
      "type": "coreclr",
      "request": "attach"
    }
  ]
}

enter image description here

2) open new terminal and run

azurite or azurite -l .\local

3) open new terminal and go to the directory of function project where your function .proj file lies. In my case it was like

cd .\project\myfunction\

enter image description here

4) Run func start --verbose

5) Click Run and Debug icon in the left sidebar

6) From the dropdown select .Net Core Attach enter image description here

7) Click the play green icon to the left of the above dropdown. enter image description here

8) Attach debugger to your function and call from postman etc. The default url is http://localhost:7071/api/

9) Add your function name at the end like http://localhost:7071/api/myfunctionname

Dispense answered 25/4 at 19:18 Comment(0)
T
1

On Rider it works after deleted bin and obj folders from previous project after a refactoring

Tetracycline answered 8/5 at 14:12 Comment(0)
A
0

I encountered this issue after installing VS Enterprise, even though I had previously been using the free version. For me, updating the Microsoft.Azure.Functions.Worker.Sdk to the latest version resolved the problem.

Acetic answered 15/9, 2023 at 9:7 Comment(1)
@Manfred I don't know if this is your case, but the issue was with the newest Visual Studio, so I had to update the library after months of not updating VS.Acetic
C
0

For me it was a missing package. Microsoft.Azure.WebJobs.Extensions Fixed after a lot of pain

Crowded answered 24/2 at 2:6 Comment(1)
unfortunately it did not workEmpery
W
-1

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.

Waterish answered 8/8, 2023 at 9:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.