C
C#•3mo ago
mixels

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
{
"WebService": {
"BindUrls": "http://0.0.0.0:8180"
}
}
{
"WebService": {
"BindUrls": "http://0.0.0.0:8180"
}
}
Program.cs
c#
var configuration = new ConfigurationBuilder()
.AddEnvironmentVariables()
.AddCommandLine(args);

if (Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") != null && Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT").Equals("Development"))
{
configuration.AddJsonFile("appsettings.Development.json");
}
else
{
configuration.AddJsonFile("appsettings.json");
}

var configRoot = configuration.Build();

IHostBuilder builder = Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(config =>
{
config.Sources.Clear();
config.AddConfiguration(configRoot);
})
.ConfigureWebHost((config) =>
{
# PROBLEM HERE -> bindUrls is null
string[] bindUrls = configRoot.GetSection("WebService").GetValue<string[]>("BindUrls");
config.UseUrls(bindUrls)
.UseKestrel()
.Configure((configure) =>
{
configure.UseMiddleware(typeof(WebHostMiddleware));
});
});
c#
var configuration = new ConfigurationBuilder()
.AddEnvironmentVariables()
.AddCommandLine(args);

if (Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") != null && Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT").Equals("Development"))
{
configuration.AddJsonFile("appsettings.Development.json");
}
else
{
configuration.AddJsonFile("appsettings.json");
}

var configRoot = configuration.Build();

IHostBuilder builder = Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(config =>
{
config.Sources.Clear();
config.AddConfiguration(configRoot);
})
.ConfigureWebHost((config) =>
{
# PROBLEM HERE -> bindUrls is null
string[] bindUrls = configRoot.GetSection("WebService").GetValue<string[]>("BindUrls");
config.UseUrls(bindUrls)
.UseKestrel()
.Configure((configure) =>
{
configure.UseMiddleware(typeof(WebHostMiddleware));
});
});
7 Replies
Jamie Brown
Jamie Brown•3mo ago
In
configRoot.GetSection("WebService").GetValue<string[]>("BindUrls");
configRoot.GetSection("WebService").GetValue<string[]>("BindUrls");
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.
{
"WebService": {
"BindUrls": [
"http://0.0.0.0:8180"
]
}
}
{
"WebService": {
"BindUrls": [
"http://0.0.0.0:8180"
]
}
}
mixels
mixelsOP•3mo ago
: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. 🙂
Jamie Brown
Jamie Brown•3mo ago
No problem, I've also been learning it recently and had a similar issue yesterday haha!
mixels
mixelsOP•3mo ago
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:
Jamie Brown
Jamie Brown•3mo ago
{[WebService:BindUrls:0, http://0.0.0.0:8180]}
Is that varbatim the json? Looks like it's wrapped in an array?
mixels
mixelsOP•3mo ago
O I think this did put me on the right track though, it seems the way you access arrays is just... strange.
c#
IConfigurationSection myArraySection = _config.GetSection("MyArray");
var itemArray = myArraySection.AsEnumerable();
c#
IConfigurationSection myArraySection = _config.GetSection("MyArray");
var itemArray = myArraySection.AsEnumerable();
Jamie Brown
Jamie Brown•3mo ago
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 😅
Want results from more Discord servers?
Add your server