How to get settings from an appsettings.json
file in a .NET 6 console application?
program.cs
file:
public class Program
{
private static ManualResetEvent _quitEvent = new ManualResetEvent(false);
private static void Main(string[] args)
{
// Setup Host
var host = CreateDefaultBuilder().Build();
host.Run();
}
private static IHostBuilder CreateDefaultBuilder()
{
return Host.CreateDefaultBuilder()
.ConfigureAppConfiguration(app =>
{
app.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
})
.ConfigureServices(services =>
{
// this is the line that has the issue
services.Configure<MailSettings>(services.Configuration.GetSection("MailSettings"));
});
}
}
The line above throws an error:
Error CS1061
'IServiceCollection' does not contain a definition for 'Configuration' and no accessible extension method 'Configuration' accepting a first argument of type 'IServiceCollection' could be found (are you missing a using directive or an assembly reference?)
How to configure it properly?