-
Notifications
You must be signed in to change notification settings - Fork 10k
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 minimal host to be created without default HostBuilder behavior #32485
Comments
4 and 5 appear to be the same config above? |
Fixed |
Thanks for contacting us. We're moving this issue to the |
API review will tolerate CreateEmptyBuilder as an API here given the goals here. |
I am one of the people who would still like it, so +1! also I'm for CreateBuilder vs CreateBuilderWithDefaults or something like that, because it explicitly says what's the intention here without the confusion. Considering that the WebApplication api is not yet in any stable version of aspnetcore, it's still open for such changes, isn't it? |
also my non trimming usecase is: for example I would like to create a local webapp, the one that listens on localhost and it makes no sense for it to be exposed or have a reverse proxy in front or actually even have most of the config knops or files. And it would have config in some user profile directory. And things like that. I don't need things like the iis integration, forwarded headers, etc, even for the theoretical future, so I would prefer not to bring all these. |
@DamianEdwards suggested adding a boolean to the existing CreateBuilder API |
well... could be. I am not even sure my first idea wasn't like that :) but I'd be okay with such a bool. |
What I suggested: namespace Microsoft.AspNetCore.Builder
{
public partial class WebApplication
{
+ public static WebApplicationBuilder CreateBuilder(string[] args = null); // Calls overload below with useDefaults=true
+ public static WebApplicationBuilder CreateBuilder(bool useDefaults, string[] args = null);
}
} |
what about only one overload and useDefaults as an optional parameter set to true by default? Is that even acceptable? considering it doesn't have to be compatible with anything... |
It is. namespace Microsoft.AspNetCore.Builder
{
public partial class WebApplication
{
+ public static WebApplicationBuilder CreateBuilder(string[] args = null, bool useDefaults = true);
}
} |
By the way, a parameter might make it hard for trim-analysis for the scenario @webczat laid out. The trimmer would not be able to discern that pieces are unused unless we introduce a feature flag. Having an explicit overload helps that case. |
ooh. Wouldn't think about that. |
We think this still needs more work: // Useful but minimal
WebApplication
.CreateBuilder()
// Kitchen-sink. Needs more work
WebApplication
.CreateMaximalBuilder()
// Full control
WebApplication
.CreateEmptyBuilder() |
I still prefer the parameter-based approach personally: // Useful defaults
WebApplication.CreateBuilder()
// Empty builder
WebApplication.CreateBuilder(useDefaults: false) What is the kitchen-sink/maximal builder about? Why do I want that and what is in it? |
well also maximal builder name looks soooo bad to me... maybe let's just stay with two paths, not three? |
@DamianEdwards but it defeats the trimming so it's a no-go. @webczat |
on a bit unrelated note, empty host is empty, it does not even have host configuration like the normal one does? so I would need to call host's methods directly for it, then use the normal Config property for the app one? |
Well but here I see three variants of CreateBuilder. so not sure... |
@davidfowl I thought the trimming issue was only with params that have a default value? I'm not suggesting that, I was suggesting concrete overloads. |
API review: rejected, we'll look into linker flags |
@eerhardt we need some assistance with linker flags to understand what's possible to do here. |
what does that mean? |
Take a look at https://github.com/dotnet/designs/blob/main/accepted/2020/feature-switch.md and https://github.com/dotnet/designs/blob/main/accepted/2020/linking-libraries.md#feature-switches to get an idea on what is possible to do with feature switches. Although, they are mainly used when you don't have a public API that can control what is pulled in, and what isn't. A concrete example is the My opinion would be to try to figure out a new public method that could be added that did the intended behavior. That seems like the more appropriate design here. Feature switches are hard to discover, and they aren't part of the code. It is less surprising for developers if they called method |
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:
|
I've updated the top post with my current API proposal and marked this "ready for review". Please take a look and let me know if you have any feedback. |
API Review Notes:
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);
}
}
namespace Microsoft.Extensions.Hosting
{
public class GenericHostWebHostBuilderExtensions
{
public static IHostBuilder ConfigureWebHost(this IHostBuilder builder, Action<IWebHostBuilder> configure)
public static IHostBuilder ConfigureWebHost(this IHostBuilder builder, Action<IWebHostBuilder> configure, Action<WebHostBuilderOptions> configureWebHostBuilder)
+ public static IHostBuilder ConfigureSlimWebHost(this IHostBuilder builder, Action<IWebHostBuilder> configure, Action<WebHostBuilderOptions> configureWebHostBuilder)
}
} When creating an "slim" web application, the following features will be on (✅) or off (❌) by default:
API Approved for preview1! We should have a lot of time to collect feedback from the community. |
SLIM 😄 |
Note on
Similar story for |
about routing middleware, isn't it that it's not added unless you call the map family of functions? or maybe it's only not added if you add it manually. |
Chicken and egg problem. The Map functions are defined on the application and routing needs to be added so that they can be used. |
@eerhardt It should be possible to turn on these features after the fact. |
I've updated the top proposal's |
This adds a new Hosting API to reduce startup and app size, and ensures the default behavior is NativeAOT compatible. Fix dotnet#32485
…#46040) * Allow minimal host to be created without default HostBuilder behavior This adds a new Hosting API to reduce startup and app size, and ensures the default behavior is NativeAOT compatible. Fix #32485 * Use the new slim hosting API in the api template. Refactor the WebHostBuilder classes to share more code.
@eerhardt I still think we should do CreateEmptyBuilder |
💯 |
Agreed. Especially now that we added stuff back to "SlimBuilder" to make it more convenient. I can open a new issue specifically for it. |
I opened #48811 for CreateEmptyBuilder. |
Background and Motivation
The default host builder pulls in a ton of dependencies that are rooted for trimming by default. We should have a mode where the minimal host is dependency minimal as well.
These numbers are native AOT net8.0 compiled on linx-x64:
WebApplication.CreateBuilder()
= 18.7 MBWe can stick to the current naming convention and use the existing Create/CreateBuilder methods as the one with the defaults and CreateEmptyBuilder to be the empty one:
Proposed API
API Usage
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, in order to reduce app size.
Alternative Designs
We could create whole new class names for another type of "Application" than a "WebApplication". Something like:
Other possible names include:
Risks
Potential confusion about which one to call. The template should make it obvious that the "empty" builder doesn't have anything in it, and you need to add things like appsettings, console logging, etc.
The text was updated successfully, but these errors were encountered: