Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Environment variables vs build configurations for .NET Core configuration [closed]

what is better to use for configuring .net core application for development and production environments: environment variables or build configurations? I.e. I can bound appsettings.*environment*.json to both of this ways

like image 905
Alex Zaitsev Avatar asked Sep 19 '25 05:09

Alex Zaitsev


1 Answers

Build configurations are a hold-over from the .NET Framework days. While you can somewhat use them in a .NET Core application, their utility is limited. About the only use is directives where you do something like:

#if DEBUG
// stuff
#endif

However, that's a questionable practice, to begin with.

With .NET Core, the paradigm is that you publish once for all environments. The concept of an environment is independent of the code itself, as it should be. This ensures that your code actually works as it should as you graduate from dev/test to staging/QA, and finally to production because the code is the code.

That aside, and since you're specifically asking about configuration, build-based config doesn't work in .NET Core. There is no app.config/web.config, and even when there is a web.config, specifically to support hosting in IIS, it's not actually utilized by .NET Core in any way - just IIS.

As such, the question is moot. For configuration in .NET Core, you use Microsoft.Extensions.Configuration, which itself relies on the concept of the environment also being config-based, rather than build-based. You don't have to an environment variable, actually. The environment can be passed through the command line, as well. The point is that it's set a run-time, not compile-time.

like image 94
Chris Pratt Avatar answered Sep 20 '25 21:09

Chris Pratt