Stringer Bell
Stringer Bell

Reputation: 265

Azure Function Dependency Injection - Using Environment.GetEnvironmentVariable Inside FunctionsStartup

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?

Upvotes: 3

Views: 1664

Answers (1)

suziki
suziki

Reputation: 14088

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");

Upvotes: 2

Related Questions