Adding appsettings.json to a .NET Core console app

This is something that strangely doesn’t seem to be that well documented and took me a while to figure out though in the end it’s pretty simple.

All that’s required is to add the following NuGet packages and an appsettings.json file.

  • Microsoft.Extensions.Configuration
  • Microsoft.Extensions.Configuration.FileExtensions
  • Microsoft.Extensions.Configuration.Json

The appsettings.json files “Copy to Output Directory” property should also be set to “Copy if newer” so that the application is able to access it when published.

The settings are injected in the main method rather than in the startup method as with web apps but the code is essentially the same.

Program.cs


static void Main(string[] args)
{
	var builder = new ConfigurationBuilder()
		.SetBasePath(Directory.GetCurrentDirectory())
		.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

	IConfigurationRoot configuration = builder.Build();

	Console.WriteLine(configuration.GetConnectionString("Storage"));
}

appsettings.json


{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "ConnectionStrings": {
    "Storage": "STORAGE-CONNECTION-STRING"
  }
}

An example of this in action can be seen here.

In the case of receiving the error “IConfigurationBuilder does not contain a definition for AddJsonFile” just rebuild the project and close and re-open Visual Studio.


27 Comments

Sergey · 17th March 2018 at 10:28 am

Thank you for sharing this!

gerardo · 22nd March 2018 at 8:01 pm

cool post! heads up i had to set appsettings.json property Copy to Output Directory to Copy If Newer for mine to work

    Shinigami · 23rd March 2018 at 9:44 am

    Thanks Gerardo, I always forget about this! Have updated the post to mention it.

murat mert · 10th April 2018 at 3:31 pm

hi,
what if there are other enviroments, i mean how can i distinguish other environments like “appsettings.*.json”.Another question does support array value of key.

    Shinigami · 11th April 2018 at 9:35 am

    Hi Murat,
    The below is used in the web application template to load the settings based on the environment.

    .AddJsonFile($”appsettings.{Environment.GetEnvironmentVariable(“ASPNETCORE_ENVIRONMENT”) ?? “Production”}.json”, optional: true)

    It looks like System.Enviroment is usable in console applications so you may be able to use this to determine what enviroment your application is running in.

PinoyDev · 9th May 2018 at 4:00 pm

Thank you so much for this!! great help

Christian Aranda · 15th September 2018 at 10:23 pm

Many thanks!!!!

Ed · 12th October 2018 at 3:57 am

Great post, thanks for the help! It is the same thing also for adding .AddEnvironmentVariables() -> Microsoft.Extensions.Configuration.EnvironmentVariables ­čÖé

luis · 31st October 2018 at 10:05 pm

Thanks for sharing!

Marcel · 7th November 2018 at 8:03 am

Hi thanks for this. How do you use it in another class? I use a static reference, but it feels kind of odd. Do you have a better idea?

Eric · 8th November 2018 at 8:23 pm

I really appreciate this. I couldn’t find it anywhere in any of Microsoft’s tutorials. I don’t know how MS expects anyone to claim .Net Core cuts dev cycles if they don’t publish really important stuff like this.

Kwesi Dadson · 20th November 2018 at 9:26 am

Thank you very very much. This is just what I needed!

Johan Bj├Ârklund · 20th November 2018 at 9:23 pm

Jolly good m8, thanks a bunch!

Les Kinney · 28th November 2018 at 4:44 pm

I’ve implemented this solution on my development machine using VS 2017 and .net core 2.1. However, when I try to build it on my build server, it fails because it requires .net Standard. In checking dependencies on the Microsoft.Extensions.Configuration packages they do require .Net Standard. Is there no way to use configuration files(setting) on just .Net Core?

    Shinigami · 29th November 2018 at 9:58 am

    Hi Les, I didn’t realise it had a dependency on .NET Standard but as Microsoft.Extensions.Configuration contains the IConfigurationRoot object I don’t think you can get away without it. .NET Standard will need to be installed on your build machine to allow you to build your solution, however if you publish it on your live machine all dependencies should be output to the publish folder when the project is published so as long as you copy the whole thing over you should be able to run your dll.

Krakor · 3rd December 2018 at 3:21 pm

T’es juste le meilleur en fait

Chad Kuehn · 4th January 2019 at 5:54 am

Helpful. Thanks!

MIke May · 8th January 2019 at 8:57 am

Many thanks. Very useful

Yasin Co┼čkun · 20th January 2019 at 10:41 pm

Hello that was very helpful but i’m really new to start coding in a empty project so i dont even know how to put mu connection string in this json file can you please help me

    Shinigami · 21st January 2019 at 9:41 am

    The appsettings.json file is just a standard JSON file in the project root. I’ve described this a bit more in the readme for the project mentioned above.

    https://github.com/mattosaurus/AzureBackup

    If you create a brand new web application project then you’ll see it has an appsettings.json file included which may help you understand how it works.

ssurba · 15th February 2019 at 4:38 pm

This is brilliant, thank you.

Deepak · 6th March 2019 at 2:10 am

Can you please share your json file as well.. need to see how does this looks like..

    Shinigami · 6th March 2019 at 9:26 am

    I’ve added an example appsettings.json file to the post.

P Fins · 19th March 2019 at 2:14 pm

Thanks. Super helpful. And still applicable to Asp.net Core 3.0… (Just in case someone else is looking…)

F Abbas · 9th May 2019 at 4:05 pm

Thanks for sharing

Brad · 16th July 2019 at 5:01 pm

I know there must be a reason, but why is this so hard in core? Every other coding framework has a way to do this easily.

Leave a Reply

Your email address will not be published. Required fields are marked *