Azure Functions settings not consistent betwen por

2019-07-23 06:59发布

问题:

Im working with Azure functions and have a problem. I declared a local.settings.json file with my variables as follows:

{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "TopicEndpoint": "my endpoint"
  }  
}

This allows my azure function to read the settings using:

var config = new ConfigurationBuilder()
        .SetBasePath(context.FunctionAppDirectory)
        .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
        .AddEnvironmentVariables()
        .Build();
var myTopic = config["Values:TopicEndpoint"];

This allows me to publish and export my variables to the portal via:

func azure functionapp publish myfunctionapp --publish-local-settings -i

However, upon publishing and verifying that the value is in the 'Application Settings' in the portal, the "Values:TopicEndpoint" doesn't exist.

In order to be able to access its value i have to put my variables directly under the json root:

{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
  }  
  "TopicEndpoint": "my endpoint"
}

That way I can safely use config['TopicEndpoint'] both in my local development environment, as well as on Azure. However, this defeats the purpose of the --publish-local-settings -i as it only exports the values found under the 'Values' key, so I have to create all my settings manually.

Do you know why this happens or if maybe Im missing something?

回答1:

I guess you use it a bit in a different way it should be. The thing is that the values from the local.settings.json come as environment variables automatically, so you don't have to worry about working with the file at all. That's how we do it on our project and it works on all environments.

// see, just env variables
var configuration = new ConfigurationBuilder().AddEnvironmentVariables().Build();

// and then use
var value = configuration["TopicEndpoint"];


回答2:

I found the issue. The problem was that I was adding a separate secret.settings.json as suggested here https://www.tomfaltesek.com/azure-functions-local-settings-json-and-source-control/ Therefore I was loading my configurations like this:

var config = new ConfigurationBuilder()
        .SetBasePath(context.FunctionAppDirectory)
        .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
        .AddJsonFile("secret.settings.json", optional: true, reloadOnChange: true)
        .AddEnvironmentVariables()
        .Build();

My local settings file looked like this:

{
  "Values": {
      "TopicEndpoint": "my endpoint"
  }  
}

And my secret settings like this:

{
  "Values": {
      "TopicKey": "my key"
  }  
}

This caused a conflict so I needed to remove the 'Values' key from the secret.settings.json so that it looked like this:

{
  "TopicKey": "my key"
}

This way I am able to both use func azure functionapp publish myfunctionapp --publish-local-settings -i to deploy the values inside my local settings file and use both files as environment variables.