79812713

Date: 2025-11-07 19:06:02
Score: 1
Natty:
Report link

For dotnet 8, it is even easier. In the program.cs just use:

var builder = Host.CreateApplicationBuilder(args);

This adds all preconfigured defaults - more info here: https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.hosting.host.createapplicationbuilder?view=net-9.0-pp#microsoft-extensions-hosting-host-createapplicationbuilder(system-string())

Then you can load/bind options quite easily i.e.

var builder = Host.CreateApplicationBuilder(args);
var services = builder.Services;
var config = builder.Configuration;
services.AddOptions<PostgresConfig>()
    .Bind(config.GetSection(PostgresConfig.SectionName));

var postgresConfig = config.GetRequiredSection(PostgresConfig.SectionName).Get<PostgresConfig>();

services.AddDbContextPool<GameStoreContext>(options =>
    options.UseNpgsql(postgresConfig.ConnectionString));
var app = builder.Build();
app.Run();
Reasons:
  • Probably link only (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Dayosakin