C#C
C#3y ago
Cotton

❔ ASP.NET Core Environment Variables in Dependency Injection

Hello, I have a (probably) slightly dumb question.
What's the correct/recommended way to inject an environment variable (like from docker-compose or similar) into a service class in ASP.NET Core?
Googling somehow always points me toward "multiple environments" such as test/staging/prod, but that's not what I'm looking for.
My hunch is that I'm supposed to do it in
Program.cs
when setting up DI, somehow.

In Spring I might do something like this to inject
MY_ENV_VAR
as a
String
into
MyService

@Service
class MyService(@Value("${MY_ENV_VAR}") val myEnvVar: String) { ... }
Was this page helpful?