Ols1
Ols1

Reputation: 321

Sharing / transforming appsettings across projects and environments

I got a Visual Studio solutions with multiple projects (function apps and a web API) and a datalayer that is shared between all the projects. I've set up the solution so all projects share the same config (appsettings.json) based on this article: https://andrewlock.net/sharing-appsettings-json-configuration-files-between-projects-in-asp-net-core/

All projects are based on .net core.

I've set up a build and a release pipeline for the dev environment. But I need a Test and production environment. How do I transform the shared config before releasing it to the test and production environment?

Upvotes: 0

Views: 1238

Answers (1)

Chris Pratt
Chris Pratt

Reputation: 239300

You don't. That's not how configuration works in ASP.NET Core. Configuration is overridden, not transformed. There's an order of ops to how the different configuration sources are applied, which is basically the order in which they're registered. The default is JSON < Environment-specific JSON < User Secrets < Environment Variables < Command-line Arguments.

If you need configuration to vary by environment, you're going to rely on the environment-specific JSON files (for general config) or environment variables and/or something like Azure Key Vault (for secrets). Since all of these come later in the configuration registration, any value you set there will override the values in your appsettings.json.

For things like environment-specific JSON, which is loaded is dependent on the value of ASPNETCORE_ENVIRONMENT, which can be set as an environment variable or passed as a command-line argument --environment. In either case, the value set corresponds to the {environment} portion of appsettings.{environment}.json. In other words, if you set the environment as Production, then appsettings.Production.json will be loaded into the config, if it is present. Environment variables are tied to the environment itself so are not dependent on any particular environment value.

Upvotes: 2

Related Questions