0

Let's consider the following class that I'm using for a configuration for my Azure function:

internal class Configuration
{
    private readonly IConfigurationRoot config;

    public Configuration(ExecutionContext context)
    {
        config = new ConfigurationBuilder()
            .SetBasePath(context.FunctionAppDirectory)
            .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables()
            .Build();
    }


    public bool MyFlag
    {
        get => bool.TryParse(config[nameof(MyFlag)], out var value) ? value : false;
        set => config[nameof(MyFlag)] = value.ToString();
    }
}

The function can easily read the MyFlag property out of the app settings.

But I want my function to be able to set the value of the MyFlag property in azure function app settings as well. Unfortunately, the value doesn't get changed on both Azure and local environments.

I tried to bind the property like this

        config.Bind("Values", this);

in the Configuration class constructor and it works but only on local environment. However, it doesn't work on Azure environment.

Is it possible to store the value to the app settings from azure function?

Szybki
  • 1,083
  • 14
  • 29

2 Answers2

0

You need to use the IConfiguration class to be able to retrieve values from the appSettings config or also can be called as your local.settings.json.

To access you myFlag you can do this

public MyService(IConfiguration configuration){
  // For a section 
  var emailConfig = configuration.GetSection("Email");
  // For a single value
  var myFlagVal = config["MyFlag"];
}
HariHaran
  • 3,642
  • 2
  • 16
  • 33
0

Another post with a solution exists here : Save Changes of IConfigurationRoot sections to its *.json file in .net Core 2.2

Hope it answers to your question as expected...