Skip to content

Allow empty WebApplicationBuilder to be created without default behavior #48811

@eerhardt

Description

@eerhardt

Background and Motivation

The default host builder pulls in a ton of dependencies that are rooted for trimming by default. With #32485 we added CreateSlimBuilder, which reduced the set of dependencies, but still brought some default dependencies in - for example #47664 brought in AuthN/Z and #47797 brought back in appSettings.json and logging.

We should implement the original proposal in #32485 and make a WebApplicationBuilder that doesn't have any default behavior - i.e. "empty".

Proposed API

namespace Microsoft.AspNetCore.Builder
{
    public class WebApplication
    {
       public static WebApplication Create(string[] args);
       public static WebApplicationBuilder CreateBuilder();
       public static WebApplicationBuilder CreateBuilder(string[] args);
       public static WebApplicationBuilder CreateBuilder(WebApplicationOptions options);
       public static WebApplicationBuilder CreateSlimBuilder();
       public static WebApplicationBuilder CreateSlimBuilder(string[] args);
       public static WebApplicationBuilder CreateSlimBuilder(WebApplicationOptions options);
+      public static WebApplicationBuilder CreateEmptyBuilder(WebApplicationOptions options);
    }
}

Usage Examples

WebApplicationOptions options = new() { Args = args };
WebApplicationBuilder builder = WebApplication.CreateEmptyBuilder(options);
builder.Logging.AddConsole();

WebApplication app = builder.Build();

app.UseRouting();
app.UseEndpoints(_ => { });

app.MapGet("/", () => "Hello World");

app.Run();

Features

When creating an "empty" web application, the following features will be on (✅) or off (❌) by default. Note that these features can be enabled by the app explicitly after creating the builder/application. They just won't be enabled by default.

  • Features
    • StaticWebAssets ❌
    • IHostingStartup ❌
  • Configuration
    • command line args ✅
    • appsetttings.json ❌
    • User secrets ❌
    • env variables ✅
  • Middleware
    • Routing ❌
    • Auth ❌
    • HostFiltering ❌
    • ForwardedHeaders ❌
  • Logging
    • Logging configuration support ❌
    • ConsoleLogger ❌
    • DebugLogger ❌
    • EventSourceLogger ❌
  • Servers
    • IIS in proc ❌
    • IIS out of proc ❌
    • Kestrel
      • HTTP 1 ✅
      • HTTPS ❌
      • HTTP 2 ❌
      • HTTP 3 ❌

Alternative Designs

We could add all 3 overloads for CreateEmptyBuilder. But I don't see the need to make it super easy to use this API.

Risks

It may be confusing which builder to use in your app.

Metadata

Metadata

Assignees

Labels

api-approvedAPI was approved in API review, it can be implementedarea-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions