Configuration in .Net Core 1.1 console project


Blog

Almost every application needs some kind of config. We can store it in file, database or cloud. But have we got any solution to help us manage those configs?

Configuration

Configuration builder provides a way to manage name-value configuration even from multiple sources.

As we can read from Microsoft there are configuration providers for:

  • Files ex JSON, XML
  • Command-line arguments
  • Environment variables
  • In-memory objects
  • Encrypted user store
  • Azure Key Vault
  • Custom Providers

I will focus on console project and show how to set it all up.

Firstly we have to download some packages. So add to the csproj:


 <PackageReference Include="Microsoft.Extensions.Configuration" Version="1.1.1" />
 <PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="1.1.1" />
 <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.1" />

 

See whole config HERE.

In this example I’m using Json configuration but as I mentioned before you can use any other package:

Setting up configuration builder

Next step is to set up the configuration builder.


 var builder = new ConfigurationBuilder()
 .SetBasePath(Directory.GetCurrentDirectory())
 .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);


IConfiguration configuration = builder.Build();

We create the ConfigurationBuilder, set base path for our configs and add Json file. We also use a new parameter available in .net core 1.1 reload on change which controls whether the configuration should be reloaded if the file changes.

Using IConfiguration

My JSON config looks like this:


{
"Setting1": "option1",

"ElasticSettings": {
"MaximumRetries": 3,
"WriteRepositoryUrl": "localhost:9200"
}
}

As you can see simple and nested form can be used to store settings.

Now it’s time to present how to get those settings and use in our program. There are couple of ways to do that:

Console.WriteLine($"Setting1 = {configuration["Setting1"]}");
Console.WriteLine($"ElasticRepo = {configuration["ElasticSettings:WriteRepositoryUrl"]}");

That was the simplest way to use it but not so pretty. What we can do instead is to create a class that will represent those settings:


public class ElasticSettings
{
    public string WriteRepositoryUrl { get; set; }
    public int MaximumRetries { get; set; }
}

And then bind an object to settings like this:


ElasticSettings esSettings = new ElasticSettings();

configuration.GetSection("ElasticSettings").Bind(esSettings);

Sum up

As we could see .NET Core gives us pretty nice tool to manage our configurations even if we store them in few different places. The first impression was nice and I will play with it and test it in my other projects.

If there are any questions or you just like the post please leave me a comment or send a message using Contact page 🙂
Subscribe to my RSS feed!
follow us in feedly


  • james w

    I think this maybe incorrect:
    Console.WriteLine($”Setting1 = {Configuration[„Setting1″]}”);
    (configuration should be lower cased „c”)