3

I'm building my first Function App so if I'm asking a silly question, please keep that in mind :)

I have a need to store some settings at runtime. In standard desktop apps I can easily update app.config by using:

System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

config.AppSettings.Settings["UserId"].Value = "myUserId";     
config.Save(ConfigurationSaveMode.Modified);

but in Function App I load config using ConfigurationBuilder:

var config = new ConfigurationBuilder()
    .SetBasePath(context.FunctionAppDirectory)
    .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
    .AddEnvironmentVariables()
    .Build();

and there is no way to update that config object. When developing locally I can update local.settings.json, but when running on Azure everything is loaded from Application settings (Environment variables).

My question is: how can I update a single value in Application settings that are associated with my Function App?

I found a similar question but the solution there requires registering app in AAD and it is updating all settings, not just one value.

Misiu
  • 4,738
  • 21
  • 94
  • 198
  • 1
    Can you explain why you want to store a value in appsettings? Does the value need to be persisted throughout the runtime of the function or for all coming function runs? I'm asking because maybe there is a better place to store the value instead of the application settings. – nldev Aug 14 '19 at 11:15
  • @nldev yes, the value needs to be persisted (especially when I deploy a new version of my function or restart it). I'm aware I can use SQL for settings, but as mentioned in my question I come from desktop apps world. I will have 3 functions running inside the same resource group, so they all will share the same application settings. One function (TimeTriggered) must query an external service and store returned in application settings and other function will use that value. Application settings were my first idea. Maybe there is a better place? – Misiu Aug 14 '19 at 11:31
  • Well as youre already using Azure functions, I would recommend azure table storage to store key value combinations, it's easy to get started with. (https://learn.microsoft.com/en-us/azure/visual-studio/vs-storage-aspnet5-getting-started-tables) If the settings are actually keys you can also look into azure keyvault (works very easy with Appsettings once its set up). Application settings.json, I would say is more suited for stuff like ServiceRetyAmount etc – nldev Aug 14 '19 at 12:53
  • @nldev I'll look at Table Storage, but I'd like to avoid creating additional "things" in Azure just to store a simple value. I won't be storing multiple values, just a single string. Won't this be overkill? Application Settings looks like an ideal place for such a thing. – Misiu Aug 14 '19 at 13:22
  • I get it, in your case it might be overkill. I wouldn't know if you can somehow use application settings for this though. Good luck!! – nldev Aug 14 '19 at 13:43
  • @nldev I don't know that either, that's why I created this question :) – Misiu Aug 14 '19 at 13:58
  • I've only used this in unit tests related to Azure Functions but you could try updating it from code like this: Environment.SetEnvironmentVariable("SettingsName", "SettingValue"). Not sure if that is allowed though. – Marc Aug 15 '19 at 11:31

1 Answers1

1

Take a look at the appsettings.json based approach.

Note that in-built support for appsettings.json files is not supported in Azure functions (yet). There is a github issue that tracks this.

There are few ways to do things at present. An example answer here: Azure Functions, how to have multiple .json config files

There is also a similar solution described at github: https://github.com/Azure/azure-functions-host/issues/4464#issuecomment-494367524

Turbo
  • 2,179
  • 18
  • 38