Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.net core Console application strongly typed Configuration

Tags:

c#

.net-core

On an .NET Core Console app, I'm trying to map settings from a custom appsettings.json file into a custom configuration class.

I've looked at several resources online but was not able to get the .Bind extension method work (i think it works on asp.net apps or previous version of .Net Core as most of the examples show that).

Here is the code:

 static void Main(string[] args)
    {

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

        IConfigurationRoot configuration = builder.Build();

        //this is a custom configuration object
        Configuration settings = new Configuration();

        //Bind the result of GetSection to the Configuration object
        //unable to use .Bind extension
        configuration.GetSection("MySection");//.Bind(settings);

        //I can map each item from MySection manually like this
        settings.APIBaseUrl = configuration.GetSection("MySection")["APIBaseUrl"];

        //what I wish to accomplish is to map the section to my Configuration object
        //But this gives me the error:
        //IConfigurationSection does not contain the definition for Bind
        //is there any work around for this for Console apps
        //or do i have to map each item manually?
        settings = configuration.GetSection("MySection").Bind(settings);

        //I'm able to get the result when calling individual keys
        Console.WriteLine($"Key1 = {configuration["MySection:Key1"]}");

        Console.WriteLine("Hello World!");
    }

Is there any approach to auto map the results from GetSection("MySection") to a custom object? This is for Console Application running on .NET Core 1.1

Thanks!

like image 535
Felasfaw Avatar asked Sep 02 '17 14:09

Felasfaw


People also ask

Can you use app config in .NET Core?

Application configuration in ASP.NET Core is performed using one or more configuration providers. Configuration providers read configuration data from key-value pairs using a variety of configuration sources: Settings files, such as appsettings. json.

What is Configurationbuilder in .NET Core?

Used to build key/value-based configuration settings for use in an application.


2 Answers

You need to add the NuGet package Microsoft.Extensions.Configuration.Binder to get it to work in a console application.

like image 160
Scott Chamberlain Avatar answered Sep 25 '22 17:09

Scott Chamberlain


I had to implement this recently so thought I'd add a whole working solution:

Ensure the following Nuget packages are installed:

  • Microsoft.Extensions.Configuration
  • Microsoft.Extensions.Configuration.Json
  • Microsoft.Extensions.Configuration.Binder

Add a json file and define some settings:

AppSettings.json

{
  "Settings": {
    "ExampleString": "StringSetting",
    "Number" :  1
  }
}

Bind this configuration to an object within a console app

public class Program
{
    static void Main(string[] args)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("AppSettings.json");

        var config = builder.Build();

        var appConfig = config.GetSection("Settings").Get<AppSettings>();

        Console.WriteLine(appConfig.ExampleString);
        Console.WriteLine(appConfig.Number);
    }
}

public class AppSettings
{
    public string ExampleString { get; set; }
    public int Number { get; set; }
}
like image 25
Tom Avatar answered Sep 25 '22 17:09

Tom