need some help understanding configuration comming from "Environment Variables"
so I read some docs that says I should store secrts for development using the Secrets.json service.
This works , it fetches the secret from there.
But the it says that if I also have an environment variable with the same name itll fetch it from there instead and thats how I should replace secrets in production.
But with this code I cannot get a environment variable, it just returns null even though i've created the environment variable in windows
38 Replies
How do you set the env variable?
with this window
i've tried user variable and system variable, none work
I'm not sure if you don't need to add env variables to the configuration
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-8.0#non-prefixed-environment-variables
yeah i don't think it'll load all the environment variables by default
it will do
DOTNET_
and ASPNETCORE_
prefixed ones by default
or maybe it only overrides existing values from previous configuration providerswell i do have an existing value from previous configuration provider
the secrets.json provider
see this value that I see is from the secrets.json
but I thought it would get replaced by the env variable
Only if you set it up, like shown in the docs I posted
in this docs
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-8.0
Configuration in ASP.NET Core
Learn how to use the Configuration API to configure AppSettings in an ASP.NET Core app.
it says it only takes the NON prefixed env variables
And then links to the place I linked
do you know the difference between
Default application configuration sources
and
Default host configuration sources
i dont understand what the difference is
Don't work either
Yeah, because, according to the docs, you need to bind it
The
TEST_
prefix is treated as a config segment
So you need to explicitly bind it when configuring... configurationyea look at the first picture
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Configuration.AddEnvironmentVariables(prefix:"TEST_");
var app = builder.Build();
Huh, right
Ah
But the prefix is stripped
So
configuration["testVariable"];
should workstripped?
oooh
Yes, stripped
hold on
lol nope
doesn't work either
is still null
Out of curiosity, return the whole
_configuration
See if the variable is among that, but somehow named differentlythe whole configuration is a weird thing
its like a dictionary of dictionaries
also silly question, you're sure the process is actually getting the environment variable right?
no i have no clue
can you get it with
Environment.GetVariable
or whatever the API is?i can try
i did :
var envs=Environment.GetEnvironmentVariables();
and i don't see it in there
im gonna try with a system variable instad
you know whats weird,
with var envs=Environment.GetEnvironmentVariables();
I recognize a lot of environment variables i see in my system variables but they dont have the same values
well, it's definitely not gonna work if the variable isn't even being set
you probably either have to restart your IDE after changing it or set it in the project launch settings instead
well the thing is
I read docs that says, in development you should use secrets.json
and then REPLACE THAT with environment variable in production
and now im trying to test as if I was in production
so I can't put variables in the IDE, when im releasing the thing lol
you understand?
oh shit it works after restarting
vs
And for NON PREFIXED VARIABLES I don't even have to bind it or anything.. .. all I had to do was restart VS
alright well thanks jim
yes, you still have to keep in mind how the variables get set in the first place
that happens before anything with config binding
I don't have any knowledge about that
well I mean
I was restarting the web app
everytime
but I had to restart the entire visual studio
yeah but if it's inheriting environment from the IDE it won't get updated until the IDE itself is restarted