-1

I have a console app in .Net 6 and wondering how can I have multiple appSettings files?

I want to have one for

appsettings.development.json appsettings.test.json appsettings.production.json

I will be deploying the console application onto windows servers and hooking it up to a scheduled task. Depending on which environment I want a different appsettings to be used. I am not sure how to do this.

I think I would have to create an environment variable on each server to make it use the right file but hoping there is more a coding way automatically use the right appsettings file.

I found this posting that seems to be almost what I need but it is for web apps and not console apps

Automatically set appsettings.json for dev and release environments in asp.net core?

chobo2
  • 83,322
  • 195
  • 530
  • 832

1 Answers1

0

You can pass environment variable via line argument, like this

C:\Myapp.exe --environment Development

EDT.

Then, (just because you have console app) you need to install Microsoft.Extensions.Configuration.CommandLine and Microsoft.Extensions.Configuration.Json packages from nuget.

After intall, get your line argument in IConfigurationRoot with:

var config = new ConfigurationBuilder()
        .AddCommandLine(args)
        .Build();

Retrive your environment variable:

var environmentName = config["environment"];

And finally, build your config from appsettings.json:

var myConfig = new ConfigurationBuilder()
        .AddJsonFile("appsettings.json")
        .AddJsonFile($"appsettings.{environmentName}.json")
        .Build();
Hominis_06
  • 44
  • 7
  • Will that automatically know what appsettings to use or do I have to do something else? – chobo2 Jan 18 '23 at 20:03
  • 1
    If you use `WebApplication.CreateBuilder(args)` - no, you don't need to do anything else, because the builder creates an IConfiguration, then adds `appsettings.json` to the configuration (this is the default behavior), then adds `appsettings.{yourEnvironmentVariable}.json`, that overrides `appsettings.json` settings with same keys. – Hominis_06 Jan 19 '23 at 06:57
  • Can you give an example of the code, I think I am following but would like to see some code. Also you Say WEbApplication.CreateBuilder. I am using a console app. Is that coming from a web namepsace? – chobo2 Jan 19 '23 at 15:52
  • My apologize, I misses, that you have simple console app. In this case see [this](https://stackoverflow.com/questions/61634578/using-appsettings-env-json-in-net-core-console-app) question (code examples are also included there) for appsettings initialization. Also see [this](https://andrewlock.net/how-to-set-the-hosting-environment-in-asp-net-core/) article about adding new environments names – Hominis_06 Jan 20 '23 at 08:52
  • No worries, yeah I found that stack post also. I am wondering do you know of a way to use the configuration manager, so only the correct appsettings gets generated. Right now if I had a production, Local, Test all of these would be generated when you go build. I know with the posts you linked, you could control which one gets "hit" but it would be nice to not have them generated if they don't need to be generated. – chobo2 Jan 20 '23 at 15:40
  • If they don't need to be generated, how your application find out what environment it should to be use? There are two ways to tell an application which environment variable to use. Either [set the environment variable on the system](https://andrewlock.net/how-to-set-the-hosting-environment-in-asp-net-core/) and then [retrieve it](https://stackoverflow.com/questions/61634578/using-appsettings-env-json-in-net-core-console-app), or use a lightweight version of the `WebApplication.CreateBuilder(args)` with line arguments (I edited the answer with example code). – Hominis_06 Jan 21 '23 at 18:11
  • *there are a couple more, but using them in a console application leads to terrible code IMHO – Hominis_06 Jan 21 '23 at 18:12