Skip to content

Add class-based host configurators or service collection modules #36509

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
maelgrove opened this issue Dec 3, 2018 · 3 comments
Closed

Add class-based host configurators or service collection modules #36509

maelgrove opened this issue Dec 3, 2018 · 3 comments

Comments

@maelgrove
Copy link

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());
@Dotnet-GitSync-Bot
Copy link
Collaborator

I couldn't figure out the best area label to add to this issue. Please help me learn by adding exactly one area label.

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added the untriaged New issue has not been triaged by the area owner label May 15, 2020
@analogrelay analogrelay transferred this issue from dotnet/extensions May 15, 2020
@analogrelay analogrelay added this to the Future milestone May 15, 2020
@ericstj ericstj removed the untriaged New issue has not been triaged by the area owner label Jun 22, 2020
Copy link
Contributor

Due to lack of recent activity, this issue has been marked as a candidate for backlog cleanup. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will undo this process.

This process is part of our issue cleanup automation.

@dotnet-policy-service dotnet-policy-service bot added backlog-cleanup-candidate An inactive issue that has been marked for automated closure. no-recent-activity labels Apr 9, 2025
Copy link
Contributor

This issue will now be closed since it had been marked no-recent-activity but received no further activity in the past 14 days. It is still possible to reopen or comment on the issue, but please note that the issue will be locked if it remains inactive for another 30 days.

@dotnet-policy-service dotnet-policy-service bot removed this from the Future milestone Apr 23, 2025
@github-actions github-actions bot locked and limited conversation to collaborators May 24, 2025
@dotnet-policy-service dotnet-policy-service bot removed no-recent-activity backlog-cleanup-candidate An inactive issue that has been marked for automated closure. labels May 24, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants