IConfiguration.GetSection is returning an empty IConfigurationSection

JasinCole

Well-known member
Joined
Feb 16, 2023
Messages
66
Programming Experience
1-3
What am I doing wrong? This shouldn't be that hard yet it doesn't work.

Given
C#:
{
  "LedgerAccounts": {
    "GeneralChecking": 1000,
    "PayrollChecking": 1002,
    "Savings": 1030,
    "CertificateOfDeposit": 1040,
    "ContractReceivables": 1120,
    "ServiceReceivables": 1130,
    "TradeReceivables": 1140,
    "OtherReceivables": 1190,
    "TradeAccountsPayable": 2000,
    "SalesTaxPayable": 2200,
    "StatePayrollTaxesPayable": 2310,
    "FederalUnemploymentTaxActPayable": 2330,
    "UnionBenefitsPayable": 2335,
    "HealthInsurancePayable": 2345,
    "CurrentPortionOfLongTermDebt": 2650
  }
}

I expect
C#:
private IConfiguration _configuration; //This is assigned in the constructor using DI and it has the correct provider with the correct data

IConfigurationSection section = _configuration.GetSection("LedgerAccounts"); //section has a value of null, I expect it should have a string of the values in that section

All the while this works just fine in another section of code, which proves to me the JSON file is being read and does contain the data, or this would fail.
C#:
var mssqlDatabase = new MssqlDatabase();
configuration.GetSection(nameof(MssqlDatabase)).Bind(mssqlDatabase);
 
Last edited:
Nevermind I see mistake now, the reason it wasn't working was for another reason. A reason I am not fully understanding
But microsoft docs state the following, Value is never populated when using GetSection

When GetSection returns a matching section, Value isn't populated. A Key and Path are returned when the section exists.
 
But they're different pieces of code; one is binding the config to a typed object, the other is not
 

Latest posts

Back
Top Bottom