6

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

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Kirsten
  • 15,730
  • 41
  • 179
  • 318
  • 1
    Question byside: Why are you returning `ConfigurationBuilder` not the built `IConfiguration`? – Martin Dec 21 '19 at 22:01
  • 1
    I think your appSettings.json files are not placed in your test project. Place app settings file from the actual project to test the project. – Rafaqat Ali Dec 21 '19 at 22:02
  • 1
    Kirsten, this might help you: https://weblog.west-wind.com/posts/2018/Feb/18/Accessing-Configuration-in-NET-Core-Test-Projects – Marco Dec 21 '19 at 22:05
  • 1
    Is the test failing (Assert?) or is there an exception throw with a detailed exception message. F.e. File not found because of: `optional: false`? – Martin Dec 21 '19 at 22:07
  • Thanks @Martin I updated the question to show it is the Assert that fails. I am also re-writing to use IConfiguration. – Kirsten Dec 22 '19 at 03:31
  • @Marco Thanks for that. Bind() is not working in .Net 3.1 – Kirsten Dec 22 '19 at 03:44
  • @KirstenGreed based on the shown settings there is no `FeedRead` section – Nkosi Dec 22 '19 at 04:06
  • applies to Net 6 MSTest project ? – Kiquenet Jul 06 '22 at 10:40

2 Answers2

4

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

Daniel Liuzzi
  • 16,807
  • 8
  • 52
  • 57
Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • IConfigurationRoot does not have a get method. It has GetChildren but that is not useful for binding. – Kirsten Dec 22 '19 at 04:13
  • 1
    @KirstenGreed extension method. make sure you have the necessary references. – Nkosi Dec 22 '19 at 04:14
  • 1
    @KirstenGreed review this sections specifically https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/index?view=aspnetcore-3.1#bind-to-an-object-graph – Nkosi Dec 22 '19 at 04:16
  • 1
    @KirstenGreed plus configuration module is independent of asp.net, so it can be used outside of asp.net environment. – Nkosi Dec 22 '19 at 04:18
  • @Kirsten What is FeedReadConfiguration ? winforms sample with net 6 configuration ? – Kiquenet Jul 06 '22 at 10:46
1

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

Then ops updated code will work.

pshoey
  • 11
  • 4