Skip to content

WebApplication.CreateBuilder() should allow a configure action parameter for testing purposes #62594

Open
@cristalink

Description

@cristalink

Background and Motivation

WebApplicationBuilder has several internal constructors that take the configureDefaults action, like internal WebApplicationBuilder(WebApplicationOptions options, Action<IHostBuilder>? configureDefaults = null).

The configureDefaults action seems to be used internally for testing purposes:

        // This is for testing purposes
        configureDefaults?.Invoke(bootstrapHostBuilder);

However, I am unable to configure the builder for my testing purposes. For example, see my comment in Issue 55867. To work around that issue, I needed to insert a WebHostDefaults.StaticWebAssetsKey into IConfiguration to specify a custom Static Web Assets manifest to allow testing on WSL2 (see StaticWebAssetsLoader.ResolveManifest for more details). There is no supported way to do it before WebApplication.CreateBuilder(WebApplicationOptions) is called, and it's too late after. I had to resort to reflection to call the internal WebApplicationBuilder constructor.

The public configureDefaults action could also be useful in production.

Proposed API

Please add the following methods to the WebApplication class.

public static WebApplicationBuilder CreateBuilder(WebApplicationOptions options, Action<IHostBuilder>? configureDefaults) =>
        new(options, configureDefaults);
public static WebApplicationBuilder CreateSlimBuilder(WebApplicationOptions options, Action<IHostBuilder>? configureDefaults) =>
        new(options, slim: true, configureDefaults);
public static WebApplicationBuilder CreateEmptyBuilder(WebApplicationOptions options, Action<IHostBuilder>? configureDefaults) =>
        new(options, slim: false, empty: true, configureDefaults);

That's all what's needed.

Alternative Designs

There is no alternative.

Risks

There is no risk.

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesfeature-mvc-testingMVC testing package

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions