Accessing configuration inside CreateDefaultBuilder chain
I'm trying to override the configuration implementation of IHostBuilder via the CreateDefaultBuilder method with my own using the below. Then, later in the IHostBuilder chain, I'm trying to read a value from configuration, like shown. My appsettings.json file clearly contains WebService.BindUrls, and I can see when debugging at runtime that
configRoot
contains WebService:BindUrls. Now I'm really confused. Can anyone shed some light on why bindUrls
is coming back null?
appsettings.json
Program.cs
7 Replies
In
you're trying to get a
string[]
, but in your appsettings.json
you just have it as a string value.
either you need to change what you're requesting to a string
, or if you're expecting multiple values, you need to reflect it as an array in json.
i.e.
:facepalm: Yep, that's definitely it, thanks, I've been simultaneously trying to learn using json config and better understanding IoC outside thedefault ASP template and I swear I had this as an array at one point lol. Thank you. 🙂
No problem, I've also been learning it recently and had a similar issue yesterday haha!
Ok I don't think that's the problem. I mean it's a problem, thanks for pointing it out heh, but there must be something else going on here too. I now see the value during debug is
{[WebService:BindUrls:0, http://0.0.0.0:8180]}
which is right but I'm still getting null in Program.cs. :headdesk:{[WebService:BindUrls:0, http://0.0.0.0:8180]}
Is that varbatim the json? Looks like it's wrapped in an array?O I think this did put me on the right track though, it seems the way you access arrays is just... strange.
Weird, I'm pretty sure there's some dark desrialisation magic that should be possible, however being dark desrialsation magic it's not something I fully understand 😅