aspnet / MicrosoftConfigurationBuilders

Microsoft.Configuration.Builders
MIT License
118 stars 61 forks source link

Configuration Builder is not working with environment variable #202

Closed pallavichede closed 1 year ago

pallavichede commented 2 years ago

I have set up my vaultname & vaultURI as in environment Variables but it is not ConfigBuilder does not resloved it .

I have set up both this variables Please check attached SS.

I have restarted my visual studio as well as my machine but no luck .

StephenMolloy commented 1 year ago

Sorry for the delay in responding to this. I don't see a screenshot or any code attached. But I'll try to take a guess with some assumptions I'll make based on the wording of your post.

None of the config builders in this project read settings directly from environment variables. Many (not all) settings for these config builders can be picked up from <appSettings> though. If you look at the Azure Key Vault example in the SampleWebApp, line 18 of web.config is using the token syntax to specify that the 'vaultName' should be read from "KeyVaultTestName" in appSettings. If you look closer at the <appSettings> section though, you'll see that "KeyVaultTestName" is just a dummy value. The real value ultimately gets pulled in by the "Env" config builder that is applied to the <appSettings> section...

... assuming that an environment variable named "KeyVaultTestName" exists in the process environment. Which could be another point where things get messed up. Most environments are pretty straightforward - and it sounds like you're just working in your local dev environment, which should mean it's pretty easy to ensure the correct environment variables are set. But sometimes it's not as straightforward, like in the case with windows services (ie, full IIS/WAS) or especially in Docker Windows containers, as environment variables only flow to the entry process by default.

One last potential pitfall to check is the order you apply your config builders in. They will execute in the order they are specified in the configBuilders="cb1,cb2,cb3" tag on config sections. So if your Key Vault builder is applied to <appSettings> and is trying to get it's "vaultName" from <appSettings>, which in turn should be getting that value from an environment variable... then the environment config builder needs to be listed before the key vault config builder, otherwise the value will get pulled in from the environment after the key vault builder has already run.

Hope that helps.