C
C#10mo ago
PatrickG

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
No description
38 Replies
Angius
Angius10mo ago
How do you set the env variable?
PatrickG
PatrickGOP10mo ago
with this window
No description
PatrickG
PatrickGOP10mo ago
i've tried user variable and system variable, none work
Angius
Angius10mo ago
I'm not sure if you don't need to add env variables to the configuration
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddRazorPages();

builder.Configuration.AddEnvironmentVariables(prefix: "MyCustomPrefix_");

var app = builder.Build();
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddRazorPages();

builder.Configuration.AddEnvironmentVariables(prefix: "MyCustomPrefix_");

var app = builder.Build();
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-8.0#non-prefixed-environment-variables
Jimmacle
Jimmacle10mo ago
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 providers
PatrickG
PatrickGOP10mo ago
well i do have an existing value from previous configuration provider the secrets.json provider
PatrickG
PatrickGOP10mo ago
No description
PatrickG
PatrickGOP10mo ago
see this value that I see is from the secrets.json but I thought it would get replaced by the env variable
Angius
Angius10mo ago
Only if you set it up, like shown in the docs I posted
PatrickG
PatrickGOP10mo ago
Configuration in ASP.NET Core
Learn how to use the Configuration API to configure AppSettings in an ASP.NET Core app.
PatrickG
PatrickGOP10mo ago
it says it only takes the NON prefixed env variables
Angius
Angius10mo ago
And then links to the place I linked
PatrickG
PatrickGOP10mo ago
do you know the difference between Default application configuration sources and Default host configuration sources i dont understand what the difference is
PatrickG
PatrickGOP10mo ago
No description
PatrickG
PatrickGOP10mo ago
No description
PatrickG
PatrickGOP10mo ago
No description
PatrickG
PatrickGOP10mo ago
Don't work either
Angius
Angius10mo ago
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... configuration
PatrickG
PatrickGOP10mo ago
yea look at the first picture // Add services to the container. builder.Services.AddControllersWithViews(); builder.Configuration.AddEnvironmentVariables(prefix:"TEST_"); var app = builder.Build();
Angius
Angius10mo ago
Huh, right Ah But the prefix is stripped So configuration["testVariable"]; should work
PatrickG
PatrickGOP10mo ago
stripped? oooh
Angius
Angius10mo ago
Yes, stripped
PatrickG
PatrickGOP10mo ago
hold on lol nope doesn't work either
PatrickG
PatrickGOP10mo ago
No description
PatrickG
PatrickGOP10mo ago
No description
PatrickG
PatrickGOP10mo ago
No description
PatrickG
PatrickGOP10mo ago
is still null
Angius
Angius10mo ago
Out of curiosity, return the whole _configuration See if the variable is among that, but somehow named differently
PatrickG
PatrickGOP10mo ago
the whole configuration is a weird thing its like a dictionary of dictionaries
Jimmacle
Jimmacle10mo ago
also silly question, you're sure the process is actually getting the environment variable right?
PatrickG
PatrickGOP10mo ago
no i have no clue
Jimmacle
Jimmacle10mo ago
can you get it with Environment.GetVariable or whatever the API is?
PatrickG
PatrickGOP10mo ago
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
Jimmacle
Jimmacle10mo ago
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
PatrickG
PatrickGOP10mo ago
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
Jimmacle
Jimmacle10mo ago
yes, you still have to keep in mind how the variables get set in the first place that happens before anything with config binding
PatrickG
PatrickGOP10mo ago
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
Jimmacle
Jimmacle10mo ago
yeah but if it's inheriting environment from the IDE it won't get updated until the IDE itself is restarted
Want results from more Discord servers?
Add your server