✅ What is the best way to setup app configuration using IHostBuilder?
I want to make a good building of my host configuration and make it convenient to use. My goal is to get the as much as possible:
- Convenience;
- Strongly typing;
- Won't compile against typos in keys;
I've already tried those ways to setup configuration and found them inconvenient:
1.
HostBuilderContext.Configuration["Key"]
· hardcoding keys;
2. context.Configuration.Get<Configuration>()
· calling the same method everywhere;
3. context.Configuration.Bind(configuration)
· confusing where to bind it at the first time;
Personally I'm using the third way, because it saves me from code duplication and hardcoding keys, but also brings a new problem — confusing where to bind the configuration.
Currently, I'm binding it in the IHostBuilder.ConfigureAppConfiguration
:
Ideally, I would like to redefine content.Configuration
so that its type matches Configuration
(my custom class), in order to directly get values without hardcoding, for example: context.Configuration.Token
. But I understand that this may not be possible.5 Replies
Read this
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-7.0
Options pattern in ASP.NET Core
Discover how to use the options pattern to represent groups of related settings in ASP.NET Core apps.
but tl;dr:
thank you, I got it!
Was 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.