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

Add support for global conventions on WebApplication #59983

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/DefaultBuilder/src/PublicAPI.Unshipped.txt
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!
28 changes: 25 additions & 3 deletions src/DefaultBuilder/src/WebApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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.
Copy link
Member

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.

/// </summary>
public IEndpointConventionBuilder EndpointConventions => _globalRouteGroupBuilder;

internal ApplicationBuilder ApplicationBuilder { get; }

IServiceProvider IEndpointRouteBuilder.ServiceProvider => Services;
Expand Down Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MapWhen calls won't preserve EndpointConventions?

return newBuilder;
}

Expand Down Expand Up @@ -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();
}
}
7 changes: 5 additions & 2 deletions src/DefaultBuilder/src/WebApplicationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Copy link
Member

Choose a reason for hiding this comment

The 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

<ItemGroup>
<Reference Include="Microsoft.AspNetCore" />
<Reference Include="Microsoft.AspNetCore.SignalR" />
<Reference Include="Microsoft.AspNetCore.Components.Endpoints" />
<Reference Include="Microsoft.AspNetCore.Mvc" />
<Reference Include="Microsoft.AspNetCore.Mvc.Core" />
<Reference Include="Microsoft.AspNetCore.TestHost" />
<Reference Include="Microsoft.AspNetCore.Authorization.Policy" />
<Content Include="Microsoft.AspNetCore.TestHost.StaticWebAssets.xml" CopyToOutputDirectory="PreserveNewest" />
Expand Down
Loading
Loading