How can I bind to a configuration class using ConfigurationBulder

17k views Asked by At

I want to use ConfigurationBuidler to read my appsettings.json file. I cant see what I am doing wrong.

My file is

{
  "comment": "this gets copied to bin\\debug on build. the app updates the copy. However to remember the settings you need to paste them here",
  "col1Width": "344"
}

My test fails

[TestMethod]
public void TestMethod1()
{
    var configuration = Helper.LoadConfiguration("appsettings.json");
    Assert.IsTrue(configuration.Properties.Keys.Contains("col1Width")); // fails here
}

My helper class is:

public static class Helper
{
    public static ConfigurationBuilder LoadConfiguration(string filename)
    {
        var configuration = new ConfigurationBuilder();
        var currentDirectory = System.IO.Directory.GetCurrentDirectory();
        configuration.SetBasePath(currentDirectory);
        configuration.AddJsonFile(path: filename, optional: false, reloadOnChange: true);
        configuration.Build();
        return configuration;
    }
}

Update:

I corrected the question title and updated the code as follows according to the Blog Post Marco pointed me to.

Only Bind() is not available

public static class Helper
{
    public static FeedReadConfiguration GetApplicationConfiguration( )
    {
        var configuration = new FeedReadConfiguration();
        var currentDirectory = System.IO.Directory.GetCurrentDirectory();

        var iConfig = GetIConfigurationRoot(currentDirectory);

        iConfig
            .GetSection("FeedRead")
            .Bind(configuration); // Not available

        return configuration;
    }

    public static IConfigurationRoot GetIConfigurationRoot(string outputPath)
    {
        return new ConfigurationBuilder()
            .SetBasePath(outputPath)
            .AddJsonFile("appsettings.json")
            .Build();
    }
    
}
public class FeedReadConfiguration
{
    public int Col1Width { get; set; }
}

enter image description here

2

There are 2 answers

5
Nkosi On BEST ANSWER

Based on the shown settings file, there is no "FeedRead" section

The settings would need to be read directly from the root.

using Microsoft.Extensions.Configuration;

public static class Helper {
    public static FeedReadConfiguration GetApplicationConfiguration( ) {
        
        var currentDirectory = System.IO.Directory.GetCurrentDirectory();

        var iConfig = GetIConfigurationRoot(currentDirectory);

        //Microsoft.Extensions.Configuration.Binder.dll
        FeedReadConfiguration configuration = iConfig.Get<FeedReadConfiguration>();

        return configuration;
    }

    public static IConfiguration GetIConfigurationRoot(string outputPath) {
        return new ConfigurationBuilder()
            .SetBasePath(outputPath)
            .AddJsonFile("appsettings.json")
            .Build();
    }    
}

(requires Microsoft.Extensions.Configuration.Binder and Microsoft.Extensions.Configuration.Json)

Reference Configuration in ASP.NET Core

Reference Options pattern in ASP.NET Core

0
pshoey On

for anyone finding this question match the solution is to add the nuget package Microsoft.Extensions.Configuration.Binder

Then ops updated code will work.