c# - 如何在 Azure Function App 中运行时更新应用程序设置中的单个值

标签 c# azure .net-core azure-function-app

我正在构建我的第一个函数应用程序,因此如果我问了一个愚蠢的问题,请记住这一点:)

我需要在运行时存储一些设置。 在标准桌面应用程序中,我可以轻松更新 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/

相关文章:

nuget - 在 dotnet 包中包含自定义配置文件

c# - 在没有 Azure 的 Windows 中实现单点登录

c# - WPF 的 Windows 资源管理器控件?

c# - 云服务到 Service Fabric 身份验证?

json - 在 .net core 1.0 中解析 A​​WS SNS 通知

c# - .NET Core - 全局化和本地化 - 类库

c# - 以编程方式设置下拉列表选定项

c# - 为什么第一次请求需要更多时间?

使用 cy.request() 使用 Cypress 登录 Azure B2C

azure - 数据库不包含在 Azure 网站备份中