【问题标题】:Is it possible to save Azure Function app setting from code?是否可以从代码中保存 Azure Function 应用程序设置?
【发布时间】:2020-04-30 12:01:22
【问题描述】:

让我们考虑以下用于配置 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】:

    您需要使用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"];
    }
    

    【讨论】:

      【解决方案2】:

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

      希望它能按预期回答您的问题...

      【讨论】:

        猜你喜欢
        • 2017-06-17
        • 1970-01-01
        • 1970-01-01
        • 2014-10-13
        • 1970-01-01
        • 2014-03-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多