0

I noticed that the following statement produces a discrepancy.

public static string GetValidation(this IConfiguration self, string key)
{
  IConfigurationSection section = self.GetSection(key);

  string value1 = section.Value;
  string value2 = section.GetValue<string>(key);

  return "";
}

The corresponding section in the config has correctly set value and is correctly located using the specified path.

...
"SomePath": "Some value",
"AlsoTried": 13,
"AndEven": true,
...

The first value is as expected, the content of the node. The second is null. When I tried typing to integers and booleans, I got zero and falsity, i.e. defaults (of course I changed the value in the config file to non-string, e.g. 13 and true respectively.

I've scrutinized the docs and googled the issue, coming up with nothing useful.

What am I missing here (because I'm sure like a rat's behind it's not a bug in .NET, hehe)?

Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
  • I would suggest saving the values in the settings as string and then converting them using teh GetValue method. – Jawad Dec 10 '19 at 04:15
  • What do you get when you index the config directly or the section? self["MySection:SomePath"]; section["SomePath"]; Maybe try debugging to see if anything is being bound properly or there may be something going on when the config is being built? – ataboo Dec 10 '19 at 04:35
  • 1
    What is the value of "key" ? – Simply Ged Dec 10 '19 at 04:42

2 Answers2

2

I'm going to assume you are passing test:SomeValue as your key and you configuration looks like:

"test": {
    "SomePath": "Some value",
    "AlsoTried": 13,
    "AndEven": true
}

Your call to self.GetSection(key); is returning the specific value you have asked for e.g.

var section = self.GetSection("test:SomePath");

This means section is now the "value" of that path e.g. Some value, which is why the section.Value property returns the correct data. But when you call section.GetValue<string>("test:SomePath") or just section.GetValue<string>("SomePath") then section does not contain a KeyValuePair for "SomePath", so you get null.

If you call GetValue<string>(key) on self it will return the correct value:

var section = self.GetValue<string>(key);
Simply Ged
  • 8,250
  • 11
  • 32
  • 40
  • Thumbs up and +1 for effort. It's freaking 6 in the morning in Stockholm and I've been up since 4 hours. Couldn't fall asleep but refused to ask here. At first, that is... So yes, it's my brains screwing me up. Thanks mate! – Konrad Viltersten Dec 10 '19 at 05:19
  • No worries. Glad I could help :-) – Simply Ged Dec 10 '19 at 05:22
0

the values in your configuration should be all strings. Use GetValue to convert them to their proper formats. Configuration is supposed to be a dictionary of < string Key, string Value>.

"SomePath": "Some value",
"AlsoTried": "13",
"AndEven": "true",

with the following commands, you should get the proper values and their types

section.GetValue<int>("AlsoTried"); // out type will int and value 13.
section.GetValue<bool>("AndEven"); // out type will be bool and value true

hope it helps.
Found this URL that explains about Configuration and data retrieval as well. Good read.

Jawad
  • 11,028
  • 3
  • 24
  • 37
  • Yeah, that's supposed to work. The issue I'm having is that it doesn't. As the example shows, converting a string to a string should be idiot-proof but generates a null as a result. I tried converting string'ed number, like "13", with the same result. Not sure what I'm missing. – Konrad Viltersten Dec 10 '19 at 04:30
  • the understanding was from your statement: "The first value is as expected, the content of the node". If you are able to pull one value, that means you are able to access the configurations. Do check https://stackoverflow.com/a/14964586/1390548 for looking up all sections and see which one you are supposed to look up – Jawad Dec 10 '19 at 04:42