我正在构建我的第一个函数应用程序,因此如果我问了一个愚蠢的问题,请记住这一点:)
我需要在运行时存储一些设置。
在标准桌面应用程序中,我可以轻松更新 app.config
通过使用:
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["UserId"].Value = "myUserId";
config.Save(ConfigurationSaveMode.Modified);
但在 Function App 中,我使用 ConfigurationBuilder
加载配置:
var config = new ConfigurationBuilder()
.SetBasePath(context.FunctionAppDirectory)
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
并且无法更新该配置对象。
在本地开发时,我可以更新 local.settings.json,但在 Azure 上运行时,所有内容都从应用程序设置
(环境变量)加载。
我的问题是:如何更新与我的 Function App 关联的应用程序设置中的单个值?
我找到了similar question但那里的解决方案需要在 AAD 中注册应用程序,并且它会更新所有设置,而不仅仅是一个值。
最佳答案
看看基于 appsettings.json 的方法。
请注意,Azure 函数尚不支持对 appsettings.json 文件的内置支持。有一个github issue跟踪这个。
目前做事的方法很少。这里有一个示例答案: Azure Functions, how to have multiple .json config files
github上也有描述类似的解决方案: https://github.com/Azure/azure-functions-host/issues/4464#issuecomment-494367524
关于c# - 如何在 Azure Function App 中运行时更新应用程序设置中的单个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57491552/