❔ Struggling to understand IConfiguration in CSharp(Blazor WASM)
Hi all,
Writing a blazor web assembly app in C# and in some of the examples I found for the Spotify.NET API had this line of code. I understand it displays the right if Configuration is Null but I just don't get what's going on or how to set the configuration.
2 Replies
in your Program.cs you have
var builder = WebAssemblyHostBuilder.CreateDefault(args);
which loads the appsettings.json
you can add other files with builder.Configuration.AddJsonFile(...)
or checkout some NuGetPackages allowing you to read configs from other sources 😉
you can of cause write your own ConfigurationProvider
too, if you need it
on the server-side (or desktops etc) it's common to read environment variables, secrets, config files and cli-parameters into the Configuration, to 1. read them all easily and 2. allow them to override eachother (cli-parameters override secrets override appsettings)
IConfiguration
is just an abstraction for all kinds of configurations. basically turning them into (hierarchical) key-value-pairs
you can then use it to bind more handy Option
-classes to the config to use them more easily: https://learn.microsoft.com/en-us/dotnet/core/extensions/optionsWas this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.