3

I've read a lot of posts on accessing IConfiguration inside Startup like this and I've seen people building their own ConfigurationRoot, but for my simple scenario:

  1. Access app settings from local.settings.json when developing locally
  2. Access app settings and from Azure App Settings when running in production/cloud

Is it OK to use Environment.GetEnvironmentVariable which meets both of those requirements?

2
  • Yes, the settings will be read as env var by default. It is from local.settings.json on local and it is from configuration settings on azure. Commented Mar 30, 2020 at 1:37
  • Any more problem? Commented Apr 1, 2020 at 5:43

1 Answer 1

2

Update:

Azure Function is based on Web App Sandbox, so it's env var is from Application Settings.

Have a look of Azure Function Basic of Web App Sandbox:

https://learn.microsoft.com/en-us/sandbox/functions-recipes/functions-basics

This is the way to access the env var:

https://learn.microsoft.com/en-us/sandbox/functions-recipes/environment-variables?tabs=csharp#accessing-environment-variables

This doc explains the local.settings.json is the app settings when azure function is running on local:

https://learn.microsoft.com/en-us/azure/azure-functions/functions-run-local?tabs=windows%2Ccsharp%2Cbash#local-settings-file

And this explains azure function app settings is in Configuration on azure:

https://learn.microsoft.com/en-us/azure/azure-functions/functions-how-to-use-azure-function-app-settings#settings

Original Answer:

Yes, it is ok.

Azure Function reads settings from local.settings.json on local and reads settings from Configuration settings on Azure, both of them will be read as env var.

So you can simply use

Environment.GetEnvironmentVariable("yoursettingskey");
Sign up to request clarification or add additional context in comments.

3 Comments

Can you add some supporting documentation?
@StringerBell I have update my answer, you can have a look.:)
I think the relevant part is here which says "The function app settings values can also be read in your code as environment variables." and later "When you develop a function app locally, you must maintain local copies of these values in the local.settings.json project file.". Thanks for finding the right docs!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.