Access to IConfiguration service in IHostingStartup.ConfigureServices

in modern composition start-up I need to register different service based on configuration.
How I can access IConfiguration service in ConfigureServices method?

    public void Configure(IWebHostBuilder builder)
        .ConfigureServices(services =>
            IConfiguration configuration = ?;

Thanks Gianmaria

Hi @Gianmaria,

You can access the IConfiguration via the IWebHostBuilderContext using a different ConfigureServices delegate. For example,

public void Configure(IWebHostBuilder builder) => builder
    .ConfigureServices((context,services) => services.AddSingleton<IDbConnectionFactory>(new OrmLiteConnectionFactory(
        context.Configuration.GetConnectionString("DefaultConnection") ?? ":memory:",

We use this in some of our templates if you want a working reference.

Hope that helps!

1 Like