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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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.Conventions.get -> Microsoft.AspNetCore.Builder.IEndpointConventionBuilder!
25 changes: 22 additions & 3 deletions src/DefaultBuilder/src/WebApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,20 @@ public sealed class WebApplication : IHost, IApplicationBuilder, IEndpointRouteB
internal const string GlobalEndpointRouteBuilderKey = "__GlobalEndpointRouteBuilder";

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;
}

/// <summary>
Expand Down Expand Up @@ -80,9 +85,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 Conventions => _globalRouteGroupBuilder;

internal ApplicationBuilder ApplicationBuilder { get; }

IServiceProvider IEndpointRouteBuilder.ServiceProvider => Services;
Expand Down Expand Up @@ -307,4 +317,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();
}
}
6 changes: 4 additions & 2 deletions src/DefaultBuilder/src/WebApplicationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,10 @@ 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);

// 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