Description
Is your feature request related to a problem?
Host builder configurations in projects with too many services / special case configurations (e.g. depending on the current host environment etc.) become bloated and unreadable quite fast.
Describe the solution you'd like
Either an interface which allows to provide host configurations through separate types (IConfigureWebHostBuilder
) or at least a module feature for IServiceCollection
(IServiceModule
).
Detailed description
The current IWebHostBuilder
contract only allows configuring the web host through fluent-style configuration delegates (usually in the form of Action<WebHostBuilderContext, TSomeBuilder>
). This fluent approach is very nice for small projects or when using aggregating helper extension methods (such as CreateDefaultBuilder
), but when numerous frameworks and services are involved, that might also need to be configured depending on the environment (dev, prod), the composition root becomes quite bloated, mixed with all kinds of configurations, constants, verbose registration code etc.
A nice way to clean the configuration up a bit in such a case would be configuration classes, similiar to Autofac's Module feature. What I would imagine is a small interface called IConfigureWebHostBuilder
, which has a single Configure()
method that passes either the current IWebHostBuilder
instance, or a generic variant which passes the WebHostBuilderContext
and an associated builder (IServiceCollection
, IConfigurationBuilder
). The IWebHostBuilder
interface would then have an additional method UseConfigurator
, for example. The webhost builder would then either instantiate the classes, or, if an instance is already provided, pass itself to it. Nothing much else.
That way, developers could separate many configuration concerns from each other and reduce the complexity of the configuration code.
Variant 1
public interface IConfigureWebHostBuilder {
void Configure(IWebHostBuilder builder)
}
webhostBuilder.UseConfigurator(new LoggingConfigurator());
Variant 2
public interface IConfigureBuilder<TBuilder> {
void Configure(WebHostBuilderContext context, TBuilder builder);
}
webhostBuilder.UseBuilderConfiguration(new LoggingBuilderConfiguration());
Variant 3
// this would be quite similiar to IStartup, with the possibility to having multiple of them
public interface IWebHostModule {
void Configure(IWebHostBuilder builder);
void ConfigureApp(IApplicationBuilder builder)
}
public class MvcModule : IWebHostModule {
public void Configure(IWebHostBuilder builder) {
builder.AddMvc();
}
public void ConfigureApp(IApplicationBuilder app) {
app.UseMvc();
}
}
webhostBuilder.UseModule(new MvcModule());