Skip to content

Commit fc87c7c

Browse files
committed
PR feedback
1 parent 25a030d commit fc87c7c

File tree

3 files changed

+31
-45
lines changed

3 files changed

+31
-45
lines changed

src/Http/Routing/src/Builder/EndpointRouteBuilderExtensions.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,23 @@ private static IEndpointConventionBuilder Map(
196196
ArgumentNullException.ThrowIfNull(pattern);
197197
ArgumentNullException.ThrowIfNull(requestDelegate);
198198

199-
return endpoints.GetOrAddRouteEndpointDataSource().AddRequestDelegate(pattern, requestDelegate, httpMethods);
199+
return endpoints
200+
.GetOrAddRouteEndpointDataSource()
201+
.AddRequestDelegate(pattern, requestDelegate, httpMethods, CreateHandlerRequestDelegate);
202+
203+
static RequestDelegateResult CreateHandlerRequestDelegate(Delegate handler, RequestDelegateFactoryOptions options, RequestDelegateMetadataResult? metadataResult)
204+
{
205+
var requestDelegate = (RequestDelegate)handler;
206+
207+
// Create request delegate that calls filter pipeline.
208+
if (options.EndpointBuilder?.FilterFactories.Count > 0)
209+
{
210+
requestDelegate = RequestDelegateFilterPipelineBuilder.Create(requestDelegate, options);
211+
}
212+
213+
var metadata = new List<object>(options.EndpointBuilder?.Metadata ?? Array.Empty<object>());
214+
return new RequestDelegateResult(RequestDelegateFilterPipelineBuilder.Create(requestDelegate, options), metadata);
215+
}
200216
}
201217

202218
/// <summary>
@@ -416,7 +432,9 @@ private static RouteHandlerBuilder Map(
416432
ArgumentNullException.ThrowIfNull(pattern);
417433
ArgumentNullException.ThrowIfNull(handler);
418434

419-
return endpoints.GetOrAddRouteEndpointDataSource().AddRouteHandler(pattern, handler, httpMethods, isFallback);
435+
return endpoints
436+
.GetOrAddRouteEndpointDataSource()
437+
.AddRouteHandler(pattern, handler, httpMethods, isFallback, RequestDelegateFactory.InferMetadata, RequestDelegateFactory.Create);
420438
}
421439

422440
private static RouteEndpointDataSource GetOrAddRouteEndpointDataSource(this IEndpointRouteBuilder endpoints)

src/Http/Routing/src/RequestDelegateFilterPipelineBuilder.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public static RequestDelegate Create(RequestDelegate requestDelegate, RequestDel
3535

3636
EndpointFilterDelegate filteredInvocation = async (EndpointFilterInvocationContext context) =>
3737
{
38+
Debug.Assert(EmptyHttpResultInstance != null, "Unable to get EmptyHttpResult instance via reflection.");
3839
if (context.HttpContext.Response.StatusCode >= 400)
3940
{
4041
return EmptyHttpResultInstance;

src/Http/Routing/src/RouteEndpointDataSource.cs

Lines changed: 10 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Diagnostics;
5-
using System.Diagnostics.CodeAnalysis;
65
using System.Reflection;
76
using System.Runtime.CompilerServices;
87
using Microsoft.AspNetCore.Builder;
@@ -28,7 +27,8 @@ public RouteEndpointDataSource(IServiceProvider applicationServices, bool throwO
2827
public RouteHandlerBuilder AddRequestDelegate(
2928
RoutePattern pattern,
3029
RequestDelegate requestDelegate,
31-
IEnumerable<string>? httpMethods)
30+
IEnumerable<string>? httpMethods,
31+
Func<Delegate, RequestDelegateFactoryOptions, RequestDelegateMetadataResult?, RequestDelegateResult> createHandlerRequestDelegateFunc)
3232
{
3333
var conventions = new ThrowOnAddAfterEndpointBuiltConventionCollection();
3434
var finallyConventions = new ThrowOnAddAfterEndpointBuiltConventionCollection();
@@ -42,30 +42,19 @@ public RouteHandlerBuilder AddRequestDelegate(
4242
Conventions = conventions,
4343
FinallyConventions = finallyConventions,
4444
InferMetadataFunc = null, // Metadata isn't infered from RequestDelegate endpoints
45-
CreateHandlerRequestDelegateFunc = CreateHandlerRequestDelegate
45+
CreateHandlerRequestDelegateFunc = createHandlerRequestDelegateFunc
4646
});
4747

4848
return new RouteHandlerBuilder(conventions, finallyConventions);
49-
50-
static RequestDelegate CreateHandlerRequestDelegate(Delegate handler, RequestDelegateFactoryOptions options, RequestDelegateMetadataResult? metadataResult)
51-
{
52-
var requestDelegate = (RequestDelegate)handler;
53-
54-
// Use request delegate directly when there are no filters.
55-
if (options.EndpointBuilder is null || options.EndpointBuilder.FilterFactories.Count == 0)
56-
{
57-
return requestDelegate;
58-
}
59-
60-
return RequestDelegateFilterPipelineBuilder.Create(requestDelegate, options);
61-
}
6249
}
6350

6451
public RouteHandlerBuilder AddRouteHandler(
6552
RoutePattern pattern,
6653
Delegate routeHandler,
6754
IEnumerable<string>? httpMethods,
68-
bool isFallback)
55+
bool isFallback,
56+
Func<MethodInfo, RequestDelegateFactoryOptions?, RequestDelegateMetadataResult>? inferMetadataFunc,
57+
Func<Delegate, RequestDelegateFactoryOptions, RequestDelegateMetadataResult?, RequestDelegateResult> createHandlerRequestDelegateFunc)
6958
{
7059
var conventions = new ThrowOnAddAfterEndpointBuiltConventionCollection();
7160
var finallyConventions = new ThrowOnAddAfterEndpointBuiltConventionCollection();
@@ -84,33 +73,11 @@ public RouteHandlerBuilder AddRouteHandler(
8473
RouteAttributes = routeAttributes,
8574
Conventions = conventions,
8675
FinallyConventions = finallyConventions,
87-
InferMetadataFunc = InferHandlerMetadata,
88-
CreateHandlerRequestDelegateFunc = CreateHandlerRequestDelegate
76+
InferMetadataFunc = inferMetadataFunc,
77+
CreateHandlerRequestDelegateFunc = createHandlerRequestDelegateFunc
8978
});
9079

9180
return new RouteHandlerBuilder(conventions, finallyConventions);
92-
93-
[UnconditionalSuppressMessage("Trimmer", "IL2026",
94-
Justification = "We surface a RequireUnreferencedCode in the call to the Map methods adding route handlers to this EndpointDataSource. Analysis is unable to infer this. " +
95-
"Map methods that configure a RequestDelegate don't use trimmer unsafe features.")]
96-
[UnconditionalSuppressMessage("AOT", "IL3050",
97-
Justification = "We surface a RequiresDynamicCode in the call to the Map methods adding route handlers this EndpointDataSource. Analysis is unable to infer this. " +
98-
"Map methods that configure a RequestDelegate don't use AOT unsafe features.")]
99-
static RequestDelegateMetadataResult InferHandlerMetadata(MethodInfo methodInfo, RequestDelegateFactoryOptions? options = null)
100-
{
101-
return RequestDelegateFactory.InferMetadata(methodInfo, options);
102-
}
103-
104-
[UnconditionalSuppressMessage("Trimmer", "IL2026",
105-
Justification = "We surface a RequireUnreferencedCode in the call to the Map methods adding route handlers to this EndpointDataSource. Analysis is unable to infer this. " +
106-
"Map methods that configure a RequestDelegate don't use trimmer unsafe features.")]
107-
[UnconditionalSuppressMessage("AOT", "IL3050",
108-
Justification = "We surface a RequiresDynamicCode in the call to the Map methods adding route handlers this EndpointDataSource. Analysis is unable to infer this. " +
109-
"Map methods that configure a RequestDelegate don't use AOT unsafe features.")]
110-
static RequestDelegate CreateHandlerRequestDelegate(Delegate handler, RequestDelegateFactoryOptions options, RequestDelegateMetadataResult? metadataResult)
111-
{
112-
return RequestDelegateFactory.Create(handler, options, metadataResult).RequestDelegate;
113-
}
11481
}
11582

11683
public override IReadOnlyList<RouteEndpoint> Endpoints
@@ -265,7 +232,7 @@ private RouteEndpointBuilder CreateRouteEndpointBuilder(
265232

266233
// We ignore the returned EndpointMetadata has been already populated since we passed in non-null EndpointMetadata.
267234
// We always set factoryRequestDelegate in case something is still referencing the redirected version of the RequestDelegate.
268-
factoryCreatedRequestDelegate = entry.CreateHandlerRequestDelegateFunc(entry.RouteHandler, rdfOptions, rdfMetadataResult);
235+
factoryCreatedRequestDelegate = entry.CreateHandlerRequestDelegateFunc(entry.RouteHandler, rdfOptions, rdfMetadataResult).RequestDelegate;
269236
}
270237

271238
Debug.Assert(factoryCreatedRequestDelegate is not null);
@@ -350,7 +317,7 @@ private readonly struct RouteEntry
350317
public required ThrowOnAddAfterEndpointBuiltConventionCollection Conventions { get; init; }
351318
public required ThrowOnAddAfterEndpointBuiltConventionCollection FinallyConventions { get; init; }
352319
public required Func<MethodInfo, RequestDelegateFactoryOptions?, RequestDelegateMetadataResult>? InferMetadataFunc { get; init; }
353-
public required Func<Delegate, RequestDelegateFactoryOptions, RequestDelegateMetadataResult?, RequestDelegate> CreateHandlerRequestDelegateFunc { get; init; }
320+
public required Func<Delegate, RequestDelegateFactoryOptions, RequestDelegateMetadataResult?, RequestDelegateResult> CreateHandlerRequestDelegateFunc { get; init; }
354321
}
355322

356323
[Flags]

0 commit comments

Comments
 (0)