Hi,
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)
{
builder
.ConfigureServices(services =>
{
IConfiguration configuration = ?;
....
}
}
Thanks Gianmaria
layoric
February 23, 2022, 12:02am
2
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:",
SqliteDialect.Provider)))
We use this in some of our templates if you want a working reference.
Hope that helps!
1 Like