Hello i am using a a json configuration file within a .NET Core App and i do not understand why i get the value null for subsections that are objects:
{
"tt": {
"aa":3,
"x":4
},
"Url":333,
"Config": {
"Production": {
"RedisAddress": {
"Hostname": "redis0",
"Port": 6379
},
"OwnAddress": {
"Hostname": "0.0.0.0",
"Port": 9300
}
},
"Dev": {
"RedisAddress": {
"Hostname": "redis0",
"Port": 6379
},
"OwnAddress": {
"Hostname": "0.0.0.0",
"Port": 9300
},
"Logger": "logger.txt"
}
}
}
When i try GetSection("Config") or GetSection("tt") i get the value null.It however returns the value for primitive types like in my case Url.
What is funny is that if i peek inside the configuration.Providers[0].Data i have all the content present like in the picture:
Why does it return null for object types?
Code
WebHostBuilder builder = new WebHostBuilder();
builder.UseStartup<Startup>();
string appPath = AppDomain.CurrentDomain.BaseDirectory;
string jsonPath = Path.Combine(Directory.GetParent(Directory.GetParent(appPath).FullName).FullName, "appsettings.json");
IConfiguration configuration = new ConfigurationBuilder()
.SetBasePath(appPath)
.AddJsonFile(jsonPath, optional: true, reloadOnChange: true)
.Build();
var sect = configuration.GetSection("Config");//has value null
var sect2 = configuration.GetSection("tt");//has value null
var sect3 = configuration.GetSection("Url"); has value 333
