让我们考虑以下类,我正在使用它为我的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函数将值存储到应用程序设置中?
你需要使用 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"];
}
另一个帖子的解决方案存在这里。Save Changes of IConfigurationRoot sections to its *.json file in .net Core 2.2.
希望它能回答你的问题,如你所愿......