C
C#2y ago
zobweyt

✅ 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:
internal class Program
{
private static readonly Configuration _configuration = new();

private static async Task Main(string[] args)
{
var host = Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(b => b.Build().Bind(_configuration))
.ConfigureServices((context, services) =>
{
services.AddSingleton(_configuration);
})
.Build();

await host.RunAsync();
}
}
internal class Program
{
private static readonly Configuration _configuration = new();

private static async Task Main(string[] args)
{
var host = Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(b => b.Build().Bind(_configuration))
.ConfigureServices((context, services) =>
{
services.AddSingleton(_configuration);
})
.Build();

await host.RunAsync();
}
}
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
sns
sns2y ago
Read this
sns
sns2y ago
Options pattern in ASP.NET Core
Discover how to use the options pattern to represent groups of related settings in ASP.NET Core apps.
sns
sns2y ago
but tl;dr:
// appsettings.json
"Position": {
"Title": "Editor",
"Name": "Joe Smith"
}
// appsettings.json
"Position": {
"Title": "Editor",
"Name": "Joe Smith"
}
// Program.cs
builder.Services.Configure<PositionOptions>(builder.Configuration.GetSection(PositionOptions.Position));
// Program.cs
builder.Services.Configure<PositionOptions>(builder.Configuration.GetSection(PositionOptions.Position));
// Injecting it using DI
public class FooController
{
private readonly PositionOptions _options;

public FooController(IOptions<PositionOptions> options)
{
_options = options.Value;
}
}
// Injecting it using DI
public class FooController
{
private readonly PositionOptions _options;

public FooController(IOptions<PositionOptions> options)
{
_options = options.Value;
}
}
zobweyt
zobweytOP2y ago
thank you, I got it!
Accord
Accord2y ago
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.

Did you find this page helpful?