I was led to believe that appsettings.json
and its environment counterparts (dev, staging, production) replaced Web.config transforms.
This works fine in a dev environment, where variables for those environments exist in the launchSettings.json
However, if I use the Publish feature, configuring the Publish to use the Staging build configuration which I created, no transform appears.
I've read elsewhere that you need to create Environment variables. Really! Is that what it has come to? We now have an armada of System Environment variables for Applications which may or may not (over time) be running on the IIS (or other) Web server on that box.
At the moment I am manually copying and pasting a "staging" appsettings.json
after doing a publish. I already have to copy and paste a pre-prepared Web.config file into that folder as it is no longer available to configure in the IDE (yet curiously, still gets produced during a Publish operation).
Is there another way to have maintainable transforms for deployments which is something akin to the transforms of the config files of old?
The appsettings. json file is an application configuration file used to store configuration settings such as database connections strings, any application scope global variables, etc. If you open the ASP.NET Core appsettings. json file, then you see the following code by default which is created by visual studio.
This actually confuses a lot of people as the configurations in Visual Studio don't really do anything for an ASP.NET Core app. ASP.NET Core apps are intended to be compiled once for any environment. The actual "environment" it runs in is determined via config, namely the ASPNETCORE_ENVIRONMENT
environment variable. In other words, whether you deploy as Debug/Staging/Release/Whatever in Visual Studio, it's the same app code. (The only time this is not the case is if you were to employ compiler directives in your code, like #if DEBUG ... #endif
, for example. That's not very common and honestly not recommended anyways.)
I think you misunderstood about environment variables. You can still use JSON config and such in production. The only environment variable that needs to be set is the aforementioned ASPNETCORE_ENVIRONMENT
.
Long and short, if you set ASPNETCORE_ENVIRONMENT
to "Production", then your appsettings.Production.json
will be loaded as you'd expect. There's no transformaton that occurs. Instead, the environment-specific config serves to extend and override other config.
I find this quite annoying too. I liked being able to change my solution configuration and test (locally) with production or staging config values.
Anyway, here is a low-ish friction option. There may be better out there.
appsettings.json
. Worst case scenario is that something uses all the security levels of your production service, which is better than giving development access by accident. And it means you don't need an environment variable on your production boxes, so there's also nothing to accidentally deleteappsettings.Development.json
with your dev database connection string, etc. You only need the items that are different, and you don't have to transform them, they'll overwrite any matches in appsettings.jsonlauchsettings.json
should already be setup with a profiles
> {name}
> environmentVariables
> ASPNETCORE_ENVIRONMENT
with a value of Development
. You can use this as your live test toggle. Comment out that line and you will use the Production settings.If you want to add a Staging level, add yourself a new profile and appsettings.staging.json
file. You will need to add the environment variable, but this is usually less of an issue and less of a risk than doing it on Production. I haven't thought of an easy way to use Staging and Development on the same box though
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With