-
Notifications
You must be signed in to change notification settings - Fork 10.2k
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
Add support for global conventions on WebApplication #59983
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
#nullable enable | ||
Microsoft.AspNetCore.Builder.WebApplication.EndpointConventions.get -> Microsoft.AspNetCore.Builder.IEndpointConventionBuilder! |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,17 +25,24 @@ namespace Microsoft.AspNetCore.Builder; | |
public sealed class WebApplication : IHost, IApplicationBuilder, IEndpointRouteBuilder, IAsyncDisposable | ||
{ | ||
internal const string GlobalEndpointRouteBuilderKey = "__GlobalEndpointRouteBuilder"; | ||
internal const string GlobalRouteGroupBuilderKey = "__GlobalRouteGroupBuilder"; | ||
|
||
private readonly IHost _host; | ||
private readonly List<EndpointDataSource> _dataSources = new(); | ||
private readonly GlobalEndpointRouteBuilder _globalEndpointRouteBuilder; | ||
private readonly RouteGroupBuilder _globalRouteGroupBuilder; | ||
|
||
internal WebApplication(IHost host) | ||
{ | ||
_host = host; | ||
|
||
_globalEndpointRouteBuilder = new(this); | ||
_globalRouteGroupBuilder = _globalEndpointRouteBuilder.MapGroup(string.Empty); | ||
|
||
ApplicationBuilder = new ApplicationBuilder(host.Services, ServerFeatures); | ||
Logger = host.Services.GetRequiredService<ILoggerFactory>().CreateLogger(Environment.ApplicationName ?? nameof(WebApplication)); | ||
|
||
Properties[GlobalEndpointRouteBuilderKey] = this; | ||
Properties[GlobalEndpointRouteBuilderKey] = _globalEndpointRouteBuilder; | ||
Properties[GlobalRouteGroupBuilderKey] = _globalRouteGroupBuilder; | ||
} | ||
|
||
/// <summary> | ||
|
@@ -80,9 +87,14 @@ IServiceProvider IApplicationBuilder.ApplicationServices | |
internal IDictionary<string, object?> Properties => ApplicationBuilder.Properties; | ||
IDictionary<string, object?> IApplicationBuilder.Properties => Properties; | ||
|
||
internal ICollection<EndpointDataSource> DataSources => _dataSources; | ||
internal ICollection<EndpointDataSource> DataSources => ((IEndpointRouteBuilder)_globalRouteGroupBuilder).DataSources; | ||
ICollection<EndpointDataSource> IEndpointRouteBuilder.DataSources => DataSources; | ||
|
||
/// <summary> | ||
/// Gets the <see cref="IEndpointConventionBuilder"/> for the application. | ||
/// </summary> | ||
public IEndpointConventionBuilder EndpointConventions => _globalRouteGroupBuilder; | ||
|
||
internal ApplicationBuilder ApplicationBuilder { get; } | ||
|
||
IServiceProvider IEndpointRouteBuilder.ServiceProvider => Services; | ||
|
@@ -213,6 +225,7 @@ IApplicationBuilder IApplicationBuilder.New() | |
var newBuilder = ApplicationBuilder.New(); | ||
// Remove the route builder so branched pipelines have their own routing world | ||
newBuilder.Properties.Remove(GlobalEndpointRouteBuilderKey); | ||
newBuilder.Properties.Remove(GlobalRouteGroupBuilderKey); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return newBuilder; | ||
} | ||
|
||
|
@@ -307,4 +320,13 @@ public IList<string>? Middleware | |
} | ||
} | ||
} | ||
|
||
private sealed class GlobalEndpointRouteBuilder(WebApplication application) : IEndpointRouteBuilder | ||
{ | ||
public IServiceProvider ServiceProvider => application.Services; | ||
|
||
public ICollection<EndpointDataSource> DataSources { get; } = []; | ||
|
||
public IApplicationBuilder CreateApplicationBuilder() => ((IApplicationBuilder)application).New(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -407,8 +407,11 @@ private void ConfigureApplication(WebHostBuilderContext context, IApplicationBui | |
// destination.Run(source) | ||
// destination.UseEndpoints() | ||
|
||
// Set the route builder so that UseRouting will use the WebApplication as the IEndpointRouteBuilder for route matching | ||
app.Properties.Add(WebApplication.GlobalEndpointRouteBuilderKey, _builtApplication); | ||
// Set the route builder so that UseRouting will use the RouteGroupBuilder managed | ||
// by the WebApplication as the IEndpointRouteBuilder instance | ||
var globalRouteGroupBuilder = _builtApplication.Properties[WebApplication.GlobalEndpointRouteBuilderKey]; | ||
app.Properties.Add(WebApplication.GlobalEndpointRouteBuilderKey, globalRouteGroupBuilder); | ||
app.Properties.Add(WebApplication.GlobalRouteGroupBuilderKey, _builtApplication.Properties[WebApplication.GlobalRouteGroupBuilderKey]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. extreme nit: two different coding patterns used here, local var for property value to assign to dictionary vs. inline property value. |
||
|
||
// Only call UseRouting() if there are endpoints configured and UseRouting() wasn't called on the global route builder already | ||
if (_builtApplication.DataSources.Count > 0) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remarks: Only applies to Endpoints not middleware like UseStaticFiles, etc.