bbqchickenrobot
bbqchickenrobot

Reputation: 3709

Azure Function not Able to Read/Access local.settings.json when Debugging w/ VS2017 for Mac

Empty/null value returned when attempting to read from the local.settings.json config file when debugging an Azure Function locally using VS2017 for Mac... after scanning the internet I wasn't able to determine if this is a known issue or if there is a work around. This is how I'm accessing the configuration settings:

ConfigurationManager.ConnectionStrings["connName"].ConnectionString

This works fine if the Function app is debugged on a Windows Machine (same git code base)

Upvotes: 2

Views: 1570

Answers (1)

Jerry Liu
Jerry Liu

Reputation: 17790

Azure function v2 running on runtime 2.x(.net core) doesn't support ConfigurationManager any more. See Azure Team's reply on github.

When I debug a v2 function on Windows, System.Configuration.ConfigurationErrorsException thrown. And v1 still works well as you have found.

So as @mariocatch has said, try to read enviroment variables instead.

Two options for you to refer.

  1. Read environment variables directly

    string connectionString = Environment.GetEnvironmentVariable("ConnectionStrings:connName");
    
  2. Add ExecutionContext context to your function method parameters and read local settings.

    [FunctionName("FunctionName")]
    public static void Run(...,ExecutionContext context)
    {
        var config = new ConfigurationBuilder()
            .SetBasePath(context.FunctionAppDirectory)
            .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables()
            .Build();
        string connectionString = config.GetConnectionString("connName");
        .....
    }
    

Upvotes: 2

Related Questions