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.


        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"));
        }

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.


17 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

Leave a Reply

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