Demo to add app settings to dependency injection in a console app.
It is difficult to add app settings to dependency injection in a .NET console app.
Create a generic AddAppSettings
extension method that can be called in ConfigureServices
to bind a section in appsettings.json to IConfiguration
using a strongly typed class.
- Add an
appsettings.json
file to a console app.- Set build action to Content, Copy to Output if newer.
{
"MyAppSettings": {
"StringSetting": "Hello App Settings",
"IntSetting": 42,
"BoolSetting": true
}
}
- Create a class with a name that matches the settings section.
public class MyAppSettings
{
public string StringSetting { get; set; }
public int IntSetting { get; set; }
public bool BoolSetting { get; set; }
}
- Add a
ServiceCollectionExtensions
class with a genericAddAppSettings
extension method.
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddAppSettings<TSettings>(this IServiceCollection services, IConfiguration config)
where TSettings : class
{
services.Configure<TSettings>(
config.GetSection(typeof(TSettings).Name));
services.AddTransient(sp =>
sp.GetRequiredService<IOptions<TSettings>>().Value);
return services;
}
}
- In Program.cs call
Host.CreateDefaultBuilder
, followed by.ConfigureServices
in which you callservices.AddAppSettings
, passingIConfiguration
.
var host = Host
.CreateDefaultBuilder(args)
.ConfigureServices(services =>
{
var config = services.BuildServiceProvider()
.GetRequiredService<IConfiguration>();
services.AddAppSettings<MyAppSettings>(config);
})
.Build();
- Call
host.Services.GetRequiredService
to get the settings and use them.
var settings = host.Services.GetRequiredService<MyAppSettings>();
Console.WriteLine("\nMy App Settings:");
Console.WriteLine($"String Setting: {settings.StringSetting}");
Console.WriteLine($"Int Setting: {settings.IntSetting}");
Console.WriteLine($"Bool Setting: {settings.BoolSetting}");