Skip to content
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

Allow empty WebApplicationBuilder to be created without default behavior #48811

Closed
eerhardt opened this issue Jun 14, 2023 · 3 comments · Fixed by #49246
Closed

Allow empty WebApplicationBuilder to be created without default behavior #48811

eerhardt opened this issue Jun 14, 2023 · 3 comments · Fixed by #49246
Assignees
Labels
api-approved API was approved in API review, it can be implemented area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions
Milestone

Comments

@eerhardt
Copy link
Member

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.

@eerhardt eerhardt added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Jun 14, 2023
@davidfowl davidfowl added api-ready-for-review API is ready for formal API review - https://github.com/dotnet/apireviews and removed api-suggestion Early API idea and discussion, it is NOT ready for implementation labels Jun 23, 2023
@ghost
Copy link

ghost commented Jun 23, 2023

Thank you for submitting this for API review. This will be reviewed by @dotnet/aspnet-api-review at the next meeting of the ASP.NET Core API Review group. Please ensure you take a look at the API review process documentation and ensure that:

  • The PR contains changes to the reference-assembly that describe the API change. Or, you have included a snippet of reference-assembly-style code that illustrates the API change.
  • The PR describes the impact to users, both positive (useful new APIs) and negative (breaking changes).
  • Someone is assigned to "champion" this change in the meeting, and they understand the impact and design of the change.

@davidfowl davidfowl added this to the .NET 8.0 milestone Jun 23, 2023
@halter73
Copy link
Member

API Review Notes

  • What if I don't want to read environment variables?
    • It's hard to use environment variables from launch settings if we don't, but it would be more empty not to.
    • We could add a nullable ConfigurationManager to WebApplicationOptions. We could do that later though.
  • Should Kestrel HTTP/1.1 be enabled by default?
    • We don't like forcing people to call the UseKestrelCore IHostBuilder extension method.
    • This isn't equivalent to (new HostBuilder()).ConfigureWebHost(...) which does not add a server.
    • We think this allows for H2C as well @amcasey
    • If we don't enable a server by default, you're force to call builder.Host.UseKestrelCore().
    • We could consider adding an AddKestrel IServiceCollection method later.

We want all the features listed to be ❌ except command line args which are already effectively opt-in

API Approved!

namespace Microsoft.AspNetCore.Builder
{
    public class WebApplication
    {
+      public static WebApplicationBuilder CreateEmptyBuilder(WebApplicationOptions options);
    }
}

@halter73 halter73 added api-approved API was approved in API review, it can be implemented and removed api-ready-for-review API is ready for formal API review - https://github.com/dotnet/apireviews labels Jun 26, 2023
@Tratcher
Copy link
Member

  • We could consider adding an AddKestrel IServiceCollection method later.

#48956 would also benefit from this.

@mkArtakMSFT mkArtakMSFT modified the milestones: .NET 8.0, 8.0 Jun 29, 2023
@eerhardt eerhardt self-assigned this Jul 6, 2023
eerhardt added a commit to eerhardt/aspnetcore that referenced this issue Jul 6, 2023
Allow an empty WebApplicationBuilder to be created without default behavior. There is no default configuration sources (ex. environment variables or appsettings.json files), no logging, and no web server (ex. Kestrel) configured by default. These can all be added explicitly by the app.

The following middleware can be enabled by the app:

- Routing and Endpoints, if the app calls MapXXX or registers an EndpointDataSource manually
- AuthN/Z, if the app adds the corresponding Auth services
- HostFiltering and ForwardedHeaders

Fix dotnet#48811
eerhardt added a commit that referenced this issue Jul 13, 2023
* Add WebApplication.CreateEmptyBuilder

Allow an empty WebApplicationBuilder to be created without default behavior. There is no default configuration sources (ex. environment variables or appsettings.json files), no logging, and no web server (ex. Kestrel) configured by default. These can all be added explicitly by the app.

The following middleware can be enabled by the app:

- Routing and Endpoints, if the app calls Services.AddRouting, and then MapXXX or registers an EndpointDataSource manually
- AuthN/Z, if the app adds the corresponding Auth services

Fix #48811

* Remove Routing from the "Empty" builder. It is up to the app to configure what they want. This is the Empty WebApplication.

* Respect hosting startup configuration. Refactor to share logic.
@ghost ghost locked as resolved and limited conversation to collaborators Aug 13, 2023
@amcasey amcasey added area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions and removed area-runtime labels Aug 25, 2023
@wtgodbe wtgodbe modified the milestones: 8.0, 8.0.0 Oct 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-approved API was approved in API review, it can be implemented area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants