Skip to content

Commit 3b27d80

Browse files
committed
PR feedback
1 parent e4c98a8 commit 3b27d80

File tree

3 files changed

+34
-34
lines changed

3 files changed

+34
-34
lines changed

src/Http/Http.Abstractions/src/Extensions/EndpointBuilder.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System.Diagnostics.CodeAnalysis;
54
using Microsoft.AspNetCore.Http;
65

76
namespace Microsoft.AspNetCore.Builder;
@@ -16,14 +15,7 @@ public abstract class EndpointBuilder
1615
/// <summary>
1716
/// Gets the list of filters that apply to this endpoint.
1817
/// </summary>
19-
public IList<Func<EndpointFilterFactoryContext, EndpointFilterDelegate, EndpointFilterDelegate>> FilterFactories
20-
{
21-
[RequiresDynamicCode("Filter factories generate dynamic code and aren't compatible with native AOT applications.")]
22-
get
23-
{
24-
return _filterFactories ??= new();
25-
}
26-
}
18+
public IList<Func<EndpointFilterFactoryContext, EndpointFilterDelegate, EndpointFilterDelegate>> FilterFactories => _filterFactories ??= new();
2719

2820
/// <summary>
2921
/// Gets or sets the delegate used to process requests for the endpoint.

src/Http/Routing/src/Builder/EndpointFilterExtensions.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,12 @@ namespace Microsoft.AspNetCore.Http;
1313
/// </summary>
1414
public static class EndpointFilterExtensions
1515
{
16-
internal const string FilterRequiresDynamicCodeWarning = "Filter factories generate dynamic code and aren't compatible with native AOT applications.";
17-
1816
/// <summary>
1917
/// Registers a filter onto the route handler.
2018
/// </summary>
2119
/// <param name="builder">The <see cref="RouteHandlerBuilder"/>.</param>
2220
/// <param name="filter">The <see cref="IEndpointFilter"/> to register.</param>
2321
/// <returns>A <see cref="RouteHandlerBuilder"/> that can be used to further customize the route handler.</returns>
24-
[RequiresDynamicCode(FilterRequiresDynamicCodeWarning)]
2522
public static TBuilder AddEndpointFilter<TBuilder>(this TBuilder builder, IEndpointFilter filter) where TBuilder : IEndpointConventionBuilder =>
2623
builder.AddEndpointFilterFactory((routeHandlerContext, next) => (context) => filter.InvokeAsync(context, next));
2724

@@ -32,7 +29,6 @@ public static TBuilder AddEndpointFilter<TBuilder>(this TBuilder builder, IEndpo
3229
/// <typeparam name="TFilterType">The type of the <see cref="IEndpointFilter"/> to register.</typeparam>
3330
/// <param name="builder">The <see cref="RouteHandlerBuilder"/>.</param>
3431
/// <returns>A <see cref="RouteHandlerBuilder"/> that can be used to further customize the route handler.</returns>
35-
[RequiresDynamicCode(FilterRequiresDynamicCodeWarning)]
3632
public static TBuilder AddEndpointFilter<TBuilder, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TFilterType>(this TBuilder builder)
3733
where TBuilder : IEndpointConventionBuilder
3834
where TFilterType : IEndpointFilter
@@ -67,7 +63,6 @@ public static TBuilder AddEndpointFilter<TBuilder>(this TBuilder builder, IEndpo
6763
/// <typeparam name="TFilterType">The type of the <see cref="IEndpointFilter"/> to register.</typeparam>
6864
/// <param name="builder">The <see cref="RouteHandlerBuilder"/>.</param>
6965
/// <returns>A <see cref="RouteHandlerBuilder"/> that can be used to further customize the route handler.</returns>
70-
[RequiresDynamicCode(FilterRequiresDynamicCodeWarning)]
7166
public static RouteHandlerBuilder AddEndpointFilter<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TFilterType>(this RouteHandlerBuilder builder)
7267
where TFilterType : IEndpointFilter
7368
{
@@ -81,7 +76,6 @@ public static TBuilder AddEndpointFilter<TBuilder>(this TBuilder builder, IEndpo
8176
/// <typeparam name="TFilterType">The type of the <see cref="IEndpointFilter"/> to register.</typeparam>
8277
/// <param name="builder">The <see cref="RouteHandlerBuilder"/>.</param>
8378
/// <returns>A <see cref="RouteHandlerBuilder"/> that can be used to further customize the route handler.</returns>
84-
[RequiresDynamicCode(FilterRequiresDynamicCodeWarning)]
8579
public static RouteGroupBuilder AddEndpointFilter<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TFilterType>(this RouteGroupBuilder builder)
8680
where TFilterType : IEndpointFilter
8781
{
@@ -95,7 +89,6 @@ public static TBuilder AddEndpointFilter<TBuilder>(this TBuilder builder, IEndpo
9589
/// <param name="builder">The <see cref="RouteHandlerBuilder"/>.</param>
9690
/// <param name="routeHandlerFilter">A method representing the core logic of the filter.</param>
9791
/// <returns>A <see cref="RouteHandlerBuilder"/> that can be used to further customize the route handler.</returns>
98-
[RequiresDynamicCode(FilterRequiresDynamicCodeWarning)]
9992
public static TBuilder AddEndpointFilter<TBuilder>(this TBuilder builder, Func<EndpointFilterInvocationContext, EndpointFilterDelegate, ValueTask<object?>> routeHandlerFilter)
10093
where TBuilder : IEndpointConventionBuilder
10194
{
@@ -108,7 +101,6 @@ public static TBuilder AddEndpointFilter<TBuilder>(this TBuilder builder, Func<E
108101
/// <param name="builder">The <see cref="RouteHandlerBuilder"/>.</param>
109102
/// <param name="filterFactory">A method representing the logic for constructing the filter.</param>
110103
/// <returns>A <see cref="RouteHandlerBuilder"/> that can be used to further customize the route handler.</returns>
111-
[RequiresDynamicCode(FilterRequiresDynamicCodeWarning)]
112104
public static TBuilder AddEndpointFilterFactory<TBuilder>(this TBuilder builder, Func<EndpointFilterFactoryContext, EndpointFilterDelegate, EndpointFilterDelegate> filterFactory)
113105
where TBuilder : IEndpointConventionBuilder
114106
{

src/Http/Routing/src/RouteEndpointDataSource.cs

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,6 @@ internal RouteEndpointBuilder GetSingleRouteEndpointBuilder()
111111
return CreateRouteEndpointBuilder(_routeEntries[0]);
112112
}
113113

114-
[UnconditionalSuppressMessage("Trimmer", "IL2026",
115-
Justification = "We surface a RequireUnreferencedCode in the call to the Map methods adding route handlers to this EndpointDataSource. Analysis is unable to infer this. " +
116-
"Map methods that configure a RequestDelegate don't use trimmer unsafe features.")]
117-
[UnconditionalSuppressMessage("AOT", "IL3050",
118-
Justification = "We surface a RequiresDynamicCode in the call to the Map methods adding route handlers this EndpointDataSource. Analysis is unable to infer this. " +
119-
"Map methods that configure a RequestDelegate don't use AOT unsafe features.")]
120114
private RouteEndpointBuilder CreateRouteEndpointBuilder(
121115
RouteEntry entry, RoutePattern? groupPrefix = null, IReadOnlyList<Action<EndpointBuilder>>? groupConventions = null, IReadOnlyList<Action<EndpointBuilder>>? groupFinallyConventions = null)
122116
{
@@ -196,16 +190,6 @@ private RouteEndpointBuilder CreateRouteEndpointBuilder(
196190
RequestDelegateFactoryOptions? rdfOptions = null;
197191
RequestDelegateMetadataResult? rdfMetadataResult = null;
198192

199-
// Any metadata inferred directly inferred by RDF or indirectly inferred via IEndpoint(Parameter)MetadataProviders are
200-
// considered less specific than method-level attributes and conventions but more specific than group conventions
201-
// so inferred metadata gets added in between these. If group conventions need to override inferred metadata,
202-
// they can do so via IEndpointConventionBuilder.Finally like the do to override any other entry-specific metadata.
203-
if (isRouteHandler)
204-
{
205-
rdfOptions = CreateRdfOptions(entry, pattern, builder);
206-
rdfMetadataResult = RequestDelegateFactory.InferMetadata(entry.RouteHandler.Method, rdfOptions);
207-
}
208-
209193
// Add delegate attributes as metadata before entry-specific conventions but after group conventions.
210194
var attributes = handler.Method.GetCustomAttributes();
211195
if (attributes is not null)
@@ -225,13 +209,23 @@ private RouteEndpointBuilder CreateRouteEndpointBuilder(
225209
// If no convention has modified builder.RequestDelegate, we can use the RequestDelegate returned by the RequestDelegateFactory directly.
226210
var conventionOverriddenRequestDelegate = ReferenceEquals(builder.RequestDelegate, redirectRequestDelegate) ? null : builder.RequestDelegate;
227211

212+
// Any metadata inferred directly inferred by RDF or indirectly inferred via IEndpoint(Parameter)MetadataProviders are
213+
// considered less specific than method-level attributes and conventions but more specific than group conventions
214+
// so inferred metadata gets added in between these. If group conventions need to override inferred metadata,
215+
// they can do so via IEndpointConventionBuilder.Finally like the do to override any other entry-specific metadata.
216+
if (isRouteHandler)
217+
{
218+
rdfOptions = CreateRdfOptions(entry, pattern, builder);
219+
rdfMetadataResult = InferHandlerMetadata(entry.RouteHandler.Method, rdfOptions);
220+
}
221+
228222
if (isRouteHandler || builder.FilterFactories.Count > 0)
229223
{
230224
rdfOptions ??= CreateRdfOptions(entry, pattern, builder);
231225

232226
// We ignore the returned EndpointMetadata has been already populated since we passed in non-null EndpointMetadata.
233227
// We always set factoryRequestDelegate in case something is still referencing the redirected version of the RequestDelegate.
234-
factoryCreatedRequestDelegate = RequestDelegateFactory.Create(entry.RouteHandler, rdfOptions, rdfMetadataResult).RequestDelegate;
228+
factoryCreatedRequestDelegate = CreateHandlerRequestDelegate(entry.RouteHandler, rdfOptions, rdfMetadataResult);
235229
}
236230

237231
Debug.Assert(factoryCreatedRequestDelegate is not null);
@@ -257,6 +251,28 @@ private RouteEndpointBuilder CreateRouteEndpointBuilder(
257251
}
258252

259253
return builder;
254+
255+
[UnconditionalSuppressMessage("Trimmer", "IL2026",
256+
Justification = "We surface a RequireUnreferencedCode in the call to the Map methods adding route handlers to this EndpointDataSource. Analysis is unable to infer this. " +
257+
"Map methods that configure a RequestDelegate don't use trimmer unsafe features.")]
258+
[UnconditionalSuppressMessage("AOT", "IL3050",
259+
Justification = "We surface a RequiresDynamicCode in the call to the Map methods adding route handlers this EndpointDataSource. Analysis is unable to infer this. " +
260+
"Map methods that configure a RequestDelegate don't use AOT unsafe features.")]
261+
static RequestDelegateMetadataResult InferHandlerMetadata(MethodInfo methodInfo, RequestDelegateFactoryOptions? options = null)
262+
{
263+
return RequestDelegateFactory.InferMetadata(methodInfo, options);
264+
}
265+
266+
[UnconditionalSuppressMessage("Trimmer", "IL2026",
267+
Justification = "We surface a RequireUnreferencedCode in the call to the Map methods adding route handlers to this EndpointDataSource. Analysis is unable to infer this. " +
268+
"Map methods that configure a RequestDelegate don't use trimmer unsafe features.")]
269+
[UnconditionalSuppressMessage("AOT", "IL3050",
270+
Justification = "We surface a RequiresDynamicCode in the call to the Map methods adding route handlers this EndpointDataSource. Analysis is unable to infer this. " +
271+
"Map methods that configure a RequestDelegate don't use AOT unsafe features.")]
272+
static RequestDelegate CreateHandlerRequestDelegate(Delegate handler, RequestDelegateFactoryOptions options, RequestDelegateMetadataResult? metadataResult)
273+
{
274+
return RequestDelegateFactory.Create(handler, options, metadataResult).RequestDelegate;
275+
}
260276
}
261277

262278
private RequestDelegateFactoryOptions CreateRdfOptions(RouteEntry entry, RoutePattern pattern, RouteEndpointBuilder builder)

0 commit comments

Comments
 (0)