To get an environment variable or an app setting value, use System.Environment.GetEnvironmentVariable, as shown in the following code example:

local.settings.json

{
    "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "xxxxxx",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "key01": "xxxxxx;"
  }
}
var key = Environment.GetEnvironmentVariable("key01")

Note: Microsoft recommends this method because it works both locally with local.settings.json and in Azure.

App settings can be read from environment variables both when developing locally and when running in Azure. When developing locally, app settings come from the Values collection in the local.settings.json file. In both environments, local and Azure, GetEnvironmentVariable("<app setting name>") retrieves the value of the named app setting. For instance, when you’re running locally, “My Site Name” would be returned if your local.settings.json file contains { "Values": { "WEBSITE_SITE_NAME": "My Site Name" } }.

The System.Configuration.ConfigurationManager.AppSettings property is an alternative API for getting app setting values, but we recommend that you use GetEnvironmentVariable as shown here.

Sources:

https://stackoverflow.com/questions/46714677/read-values-from-local-settings-json-in-vs-2017-azure-function-development/46722484

https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-class-library?tabs=v2%2Ccmd#environment-variables

Last modified: March 26, 2021

Author

Comments

Write a Reply or Comment