是否可以从代码中保存Azure Function应用设置?

问题描述 投票:0回答:1

让我们考虑以下类,我正在使用它为我的Azure函数进行配置。

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();
    }
}

该函数可以很容易地从应用程序设置中读取MyFlag属性。

但我希望我的函数也能在 azure 函数应用设置中设置 MyFlag 属性的值。遗憾的是,该值在Azure和本地环境中都不会被改变。

我试着像这样绑定属性

        config.Bind("Values", this);

Configuration 类构造函数,并且它工作,但只在本地环境中。但是,在Azure环境下却无法使用。

能否从Azure函数将值存储到应用程序设置中?

c# azure azure-functions appsettings
1个回答
0
投票

你需要使用 IConfiguration 类,以便能够从该类中检索值。appSettings 配置,也可以作为你的 local.settings.json.

要进入你 myFlag 你可以这样做

public MyService(IConfiguration configuration){
  // For a section 
  var emailConfig = configuration.GetSection("Email");
  // For a single value
  var myFlagVal = config["MyFlag"];
}

0
投票

另一个帖子的解决方案存在这里。Save Changes of IConfigurationRoot sections to its *.json file in .net Core 2.2.

希望它能回答你的问题,如你所愿......

© www.soinside.com 2019 - 2024. All rights reserved.