MultiAppSettings with NetCoreAppSettings

Hello,

It seams that there is an issue when using MultiAppSettings with NetCoreAppSettings.
NetCoreAppSettings is working like expected but when wrapped inside MultiAppSettings, this will return always null or throw an exception.

The problem we have is with the Get<T>(string name) and Get<T>(string name, T deafultValue) methods

Thanks,
Alex.

I’m not able to repro this, if the key exists I get the configured value with both Get APIs whether it’s the first provider in MultiAppSettings or last. If it doesn’t exist it returns the expected “DEFAULT”.

AppSettings = new MultiAppSettings(
    AppSettings, 
    new DictionarySettings());

var existsGetOrDefault = AppSettings.Get("Exists", "DEFAULT");
var existsGet = AppSettings.Get<string>("Exists");
var notExists = AppSettings.Get("NotExists", "DEFAULT");

Hi,

*** Cannot use gistlyn because i cannot add appsettings.config and appsettings.json

I have the following:

Console app,

<PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Configuration" version="3.1.0">
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" version="3.1.0">
    <PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" version="3.1.0">
</ItemGroup>

file appsettings.config

...
  < appSettings >
    < add key="SomeKey" value="....."/>
  < appSettings >
...

file appsettings.json

{
  "Server": {
    "Endpoints": {
      "Http": {
        "Url": "http://0.0.0.0:80"
      }
    }
  }
}

Classes

public interface IServerSettings
   {
       /// <summary>
       /// Chiave-valore dei end point del server.
       /// </summary>
       Dictionary<string, UrlSettings> Endpoints { get; set; }
   }

   /// <summary>
   /// Configurazione Url del server.
   /// </summary>
   public class ServerSettings : IServerSettings
   {
       /// <summary>
       /// Chiave-valore dei end point del server.
       /// </summary>
       public Dictionary<string, UrlSettings> Endpoints { get; set; }
   }

   public interface IUrlSettings
   {
       /// <summary>
       /// Il url. Url è del tipo: "http://0.0.0.0:80"
       /// </summary>
       string Url { get; set; }
   }

   /// <summary>
   /// Configurazione Url.
   /// </summary>
   public class UrlSettings : IUrlSettings
   {
       /// <summary>
       /// Il url. Url è del tipo: "http://0.0.0.0:80"
       /// </summary>
       public string Url { get; set; }
   }

Program.cs

var configuration = new ConfigurationBuilder()
                        .AddJsonFile("appsettings.json", false)
                        .Build();

//load from appsettings.json
var jsonAppSettings =  new NetCoreAppSettings(configuration);

//load from appsettings.config
var appSettingsXml = new AppSettings();

//combine jsonAppSettings and appSettingsXml 
var appSettings = new MultiAppSettings(jsonAppSettings, appSettingsXml);

//works as expected,  server.Endpoints["Http"].Url = "http://0.0.0.0:80"
var server = jsonAppSettings.Get<ServerSettings>("Server");

//server is null
server = appSettings.Get<ServerSettings>("Server");

This should now be resolved from the latest v5.9.3 now available on MyGet.