From 1987b9f4c57d45e9568708f6b717b4ad8e987653 Mon Sep 17 00:00:00 2001 From: Stephen Halter Date: Tue, 23 Mar 2021 18:18:06 -0700 Subject: [PATCH 1/6] Make RequestDelegateBuilder public - Formerly known as MapActionExpressionTreeBuilder --- ...icrosoft.AspNetCore.Http.Extensions.csproj | 1 + .../src/PublicAPI.Unshipped.txt | 2 + .../src/RequestDelegateBuilder.cs} | 27 +++++---- .../test/RouteDelegateBuilderTests.cs} | 60 +++++++++---------- ...MapActionEndpointRouteBuilderExtensions.cs | 4 +- .../src/Microsoft.AspNetCore.Routing.csproj | 1 - 6 files changed, 52 insertions(+), 43 deletions(-) rename src/Http/{Routing/src/Internal/MapActionExpressionTreeBuilder.cs => Http.Extensions/src/RequestDelegateBuilder.cs} (94%) rename src/Http/{Routing/test/UnitTests/Internal/MapActionExpressionTreeBuilderTest.cs => Http.Extensions/test/RouteDelegateBuilderTests.cs} (91%) diff --git a/src/Http/Http.Extensions/src/Microsoft.AspNetCore.Http.Extensions.csproj b/src/Http/Http.Extensions/src/Microsoft.AspNetCore.Http.Extensions.csproj index 123ae96be7a6..94a3e73732a3 100644 --- a/src/Http/Http.Extensions/src/Microsoft.AspNetCore.Http.Extensions.csproj +++ b/src/Http/Http.Extensions/src/Microsoft.AspNetCore.Http.Extensions.csproj @@ -11,6 +11,7 @@ + diff --git a/src/Http/Http.Extensions/src/PublicAPI.Unshipped.txt b/src/Http/Http.Extensions/src/PublicAPI.Unshipped.txt index c82589d1209b..abdd44314e49 100644 --- a/src/Http/Http.Extensions/src/PublicAPI.Unshipped.txt +++ b/src/Http/Http.Extensions/src/PublicAPI.Unshipped.txt @@ -152,6 +152,7 @@ Microsoft.AspNetCore.Http.Headers.ResponseHeaders.Set(string! name, object? valu Microsoft.AspNetCore.Http.Headers.ResponseHeaders.SetCookie.get -> System.Collections.Generic.IList! Microsoft.AspNetCore.Http.Headers.ResponseHeaders.SetCookie.set -> void Microsoft.AspNetCore.Http.Headers.ResponseHeaders.SetList(string! name, System.Collections.Generic.IList? values) -> void +Microsoft.AspNetCore.Http.RequestDelegateBuilder override Microsoft.AspNetCore.Http.Extensions.QueryBuilder.Equals(object? obj) -> bool override Microsoft.AspNetCore.Http.Extensions.QueryBuilder.ToString() -> string! static Microsoft.AspNetCore.Http.Extensions.HttpRequestMultipartExtensions.GetMultipartBoundary(this Microsoft.AspNetCore.Http.HttpRequest! request) -> string! @@ -168,6 +169,7 @@ static Microsoft.AspNetCore.Http.HeaderDictionaryTypeExtensions.AppendList(th static Microsoft.AspNetCore.Http.HeaderDictionaryTypeExtensions.GetTypedHeaders(this Microsoft.AspNetCore.Http.HttpRequest! request) -> Microsoft.AspNetCore.Http.Headers.RequestHeaders! static Microsoft.AspNetCore.Http.HeaderDictionaryTypeExtensions.GetTypedHeaders(this Microsoft.AspNetCore.Http.HttpResponse! response) -> Microsoft.AspNetCore.Http.Headers.ResponseHeaders! static Microsoft.AspNetCore.Http.HttpContextServerVariableExtensions.GetServerVariable(this Microsoft.AspNetCore.Http.HttpContext! context, string! variableName) -> string? +static Microsoft.AspNetCore.Http.RequestDelegateBuilder.BuildRequestDelegate(System.Delegate! action) -> Microsoft.AspNetCore.Http.RequestDelegate! static Microsoft.AspNetCore.Http.ResponseExtensions.Clear(this Microsoft.AspNetCore.Http.HttpResponse! response) -> void static Microsoft.AspNetCore.Http.ResponseExtensions.Redirect(this Microsoft.AspNetCore.Http.HttpResponse! response, string! location, bool permanent, bool preserveMethod) -> void static Microsoft.AspNetCore.Http.SendFileResponseExtensions.SendFileAsync(this Microsoft.AspNetCore.Http.HttpResponse! response, Microsoft.Extensions.FileProviders.IFileInfo! file, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task! diff --git a/src/Http/Routing/src/Internal/MapActionExpressionTreeBuilder.cs b/src/Http/Http.Extensions/src/RequestDelegateBuilder.cs similarity index 94% rename from src/Http/Routing/src/Internal/MapActionExpressionTreeBuilder.cs rename to src/Http/Http.Extensions/src/RequestDelegateBuilder.cs index 1e83d92c3497..2f1d35f5e0c6 100644 --- a/src/Http/Routing/src/Internal/MapActionExpressionTreeBuilder.cs +++ b/src/Http/Http.Extensions/src/RequestDelegateBuilder.cs @@ -11,24 +11,26 @@ using System.Reflection; using System.Threading; using System.Threading.Tasks; -using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.Metadata; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Internal; using Microsoft.Extensions.Logging; -namespace Microsoft.AspNetCore.Routing.Internal +namespace Microsoft.AspNetCore.Http { - internal static class MapActionExpressionTreeBuilder + /// + /// Builds implementations from request handlers. + /// + public static class RequestDelegateBuilder { private static readonly MethodInfo ChangeTypeMethodInfo = GetMethodInfo>((value, type) => Convert.ChangeType(value, type, CultureInfo.InvariantCulture)); - private static readonly MethodInfo ExecuteTaskOfTMethodInfo = typeof(MapActionExpressionTreeBuilder).GetMethod(nameof(ExecuteTask), BindingFlags.NonPublic | BindingFlags.Static)!; - private static readonly MethodInfo ExecuteTaskOfStringMethodInfo = typeof(MapActionExpressionTreeBuilder).GetMethod(nameof(ExecuteTaskOfString), BindingFlags.NonPublic | BindingFlags.Static)!; - private static readonly MethodInfo ExecuteValueTaskOfTMethodInfo = typeof(MapActionExpressionTreeBuilder).GetMethod(nameof(ExecuteValueTaskOfT), BindingFlags.NonPublic | BindingFlags.Static)!; - private static readonly MethodInfo ExecuteValueTaskMethodInfo = typeof(MapActionExpressionTreeBuilder).GetMethod(nameof(ExecuteValueTask), BindingFlags.NonPublic | BindingFlags.Static)!; - private static readonly MethodInfo ExecuteValueTaskOfStringMethodInfo = typeof(MapActionExpressionTreeBuilder).GetMethod(nameof(ExecuteValueTaskOfString), BindingFlags.NonPublic | BindingFlags.Static)!; - private static readonly MethodInfo ExecuteTaskResultOfTMethodInfo = typeof(MapActionExpressionTreeBuilder).GetMethod(nameof(ExecuteTaskResult), BindingFlags.NonPublic | BindingFlags.Static)!; - private static readonly MethodInfo ExecuteValueResultTaskOfTMethodInfo = typeof(MapActionExpressionTreeBuilder).GetMethod(nameof(ExecuteValueTaskResult), BindingFlags.NonPublic | BindingFlags.Static)!; + private static readonly MethodInfo ExecuteTaskOfTMethodInfo = typeof(RequestDelegateBuilder).GetMethod(nameof(ExecuteTask), BindingFlags.NonPublic | BindingFlags.Static)!; + private static readonly MethodInfo ExecuteTaskOfStringMethodInfo = typeof(RequestDelegateBuilder).GetMethod(nameof(ExecuteTaskOfString), BindingFlags.NonPublic | BindingFlags.Static)!; + private static readonly MethodInfo ExecuteValueTaskOfTMethodInfo = typeof(RequestDelegateBuilder).GetMethod(nameof(ExecuteValueTaskOfT), BindingFlags.NonPublic | BindingFlags.Static)!; + private static readonly MethodInfo ExecuteValueTaskMethodInfo = typeof(RequestDelegateBuilder).GetMethod(nameof(ExecuteValueTask), BindingFlags.NonPublic | BindingFlags.Static)!; + private static readonly MethodInfo ExecuteValueTaskOfStringMethodInfo = typeof(RequestDelegateBuilder).GetMethod(nameof(ExecuteValueTaskOfString), BindingFlags.NonPublic | BindingFlags.Static)!; + private static readonly MethodInfo ExecuteTaskResultOfTMethodInfo = typeof(RequestDelegateBuilder).GetMethod(nameof(ExecuteTaskResult), BindingFlags.NonPublic | BindingFlags.Static)!; + private static readonly MethodInfo ExecuteValueResultTaskOfTMethodInfo = typeof(RequestDelegateBuilder).GetMethod(nameof(ExecuteValueTaskResult), BindingFlags.NonPublic | BindingFlags.Static)!; private static readonly MethodInfo GetRequiredServiceMethodInfo = typeof(ServiceProviderServiceExtensions).GetMethod(nameof(ServiceProviderServiceExtensions.GetRequiredService), BindingFlags.Public | BindingFlags.Static, new Type[] { typeof(IServiceProvider) })!; private static readonly MethodInfo ResultWriteResponseAsync = typeof(IResult).GetMethod(nameof(IResult.ExecuteAsync), BindingFlags.Public | BindingFlags.Instance)!; private static readonly MethodInfo StringResultWriteResponseAsync = GetMethodInfo>((response, text) => HttpResponseWritingExtensions.WriteAsync(response, text, default)); @@ -44,6 +46,11 @@ internal static class MapActionExpressionTreeBuilder private static readonly MemberExpression HttpResponseExpr = Expression.Property(HttpContextParameter, nameof(HttpContext.Response)); private static readonly MemberExpression RequestAbortedExpr = Expression.Property(HttpContextParameter, nameof(HttpContext.RequestAborted)); + /// + /// Builds a implementation for . + /// + /// A request handler with any number of custom parameters that often produces a response with its return value. + /// The public static RequestDelegate BuildRequestDelegate(Delegate action) { // Non void return type diff --git a/src/Http/Routing/test/UnitTests/Internal/MapActionExpressionTreeBuilderTest.cs b/src/Http/Http.Extensions/test/RouteDelegateBuilderTests.cs similarity index 91% rename from src/Http/Routing/test/UnitTests/Internal/MapActionExpressionTreeBuilderTest.cs rename to src/Http/Http.Extensions/test/RouteDelegateBuilderTests.cs index 39372600077f..4e0b65c781da 100644 --- a/src/Http/Routing/test/UnitTests/Internal/MapActionExpressionTreeBuilderTest.cs +++ b/src/Http/Http.Extensions/test/RouteDelegateBuilderTests.cs @@ -22,7 +22,7 @@ namespace Microsoft.AspNetCore.Routing.Internal { - public class MapActionExpressionTreeBuilderTest + public class RouteDelegateBuilderTests { public static IEnumerable NoResult { @@ -85,7 +85,7 @@ public async Task RequestDelegateInvokesAction(Delegate @delegate) { var httpContext = new DefaultHttpContext(); - var requestDelegate = MapActionExpressionTreeBuilder.BuildRequestDelegate(@delegate); + var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate(@delegate); await requestDelegate(httpContext); @@ -136,7 +136,7 @@ public async Task RequestDelegatePopulatesFromRouteParameterBasedOnParameterName var httpContext = new DefaultHttpContext(); httpContext.Request.RouteValues[paramName] = originalRouteParam.ToString(NumberFormatInfo.InvariantInfo); - var requestDelegate = MapActionExpressionTreeBuilder.BuildRequestDelegate(@delegate); + var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate(@delegate); await requestDelegate(httpContext); @@ -179,7 +179,7 @@ public async Task RequestDelegatePopulatesFromRouteOptionalParameter(Delegate @d { var httpContext = new DefaultHttpContext(); - var requestDelegate = MapActionExpressionTreeBuilder.BuildRequestDelegate(@delegate); + var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate(@delegate); await requestDelegate(httpContext); @@ -197,7 +197,7 @@ public async Task RequestDelegatePopulatesFromRouteOptionalParameterBasedOnParam httpContext.Request.RouteValues[paramName] = originalRouteParam.ToString(NumberFormatInfo.InvariantInfo); - var requestDelegate = MapActionExpressionTreeBuilder.BuildRequestDelegate(@delegate); + var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate(@delegate); await requestDelegate(httpContext); @@ -220,7 +220,7 @@ void TestAction([FromRoute(Name = specifiedName)] int foo) var httpContext = new DefaultHttpContext(); httpContext.Request.RouteValues[specifiedName] = originalRouteParam.ToString(NumberFormatInfo.InvariantInfo); - var requestDelegate = MapActionExpressionTreeBuilder.BuildRequestDelegate((Action)TestAction); + var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate((Action)TestAction); await requestDelegate(httpContext); @@ -243,7 +243,7 @@ void TestAction([FromRoute] int foo) var httpContext = new DefaultHttpContext(); httpContext.Request.RouteValues[unmatchedName] = unmatchedRouteParam.ToString(NumberFormatInfo.InvariantInfo); - var requestDelegate = MapActionExpressionTreeBuilder.BuildRequestDelegate((Action)TestAction); + var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate((Action)TestAction); await requestDelegate(httpContext); @@ -271,7 +271,7 @@ void TestAction([FromQuery] int value) var httpContext = new DefaultHttpContext(); httpContext.Request.Query = query; - var requestDelegate = MapActionExpressionTreeBuilder.BuildRequestDelegate((Action)TestAction); + var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate((Action)TestAction); await requestDelegate(httpContext); @@ -294,7 +294,7 @@ void TestAction([FromHeader(Name = customHeaderName)] int value) var httpContext = new DefaultHttpContext(); httpContext.Request.Headers[customHeaderName] = originalHeaderParam.ToString(NumberFormatInfo.InvariantInfo); - var requestDelegate = MapActionExpressionTreeBuilder.BuildRequestDelegate((Action)TestAction); + var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate((Action)TestAction); await requestDelegate(httpContext); @@ -322,7 +322,7 @@ void TestAction([FromBody] Todo todo) var requestBodyBytes = JsonSerializer.SerializeToUtf8Bytes(originalTodo); httpContext.Request.Body = new MemoryStream(requestBodyBytes); - var requestDelegate = MapActionExpressionTreeBuilder.BuildRequestDelegate((Action)TestAction); + var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate((Action)TestAction); await requestDelegate(httpContext); @@ -341,7 +341,7 @@ void TestAction([FromBody] Todo todo) httpContext.Request.Headers["Content-Type"] = "application/json"; httpContext.Request.Headers["Content-Length"] = "0"; - var requestDelegate = MapActionExpressionTreeBuilder.BuildRequestDelegate((Action)TestAction); + var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate((Action)TestAction); await Assert.ThrowsAsync(() => requestDelegate(httpContext)); } @@ -360,7 +360,7 @@ void TestAction([FromBody(AllowEmpty = true)] Todo todo) httpContext.Request.Headers["Content-Type"] = "application/json"; httpContext.Request.Headers["Content-Length"] = "0"; - var requestDelegate = MapActionExpressionTreeBuilder.BuildRequestDelegate((Action)TestAction); + var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate((Action)TestAction); await requestDelegate(httpContext); @@ -384,7 +384,7 @@ void TestAction([FromBody(AllowEmpty = true)] BodyStruct bodyStruct) httpContext.Request.Headers["Content-Type"] = "application/json"; httpContext.Request.Headers["Content-Length"] = "0"; - var requestDelegate = MapActionExpressionTreeBuilder.BuildRequestDelegate((Action)TestAction); + var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate((Action)TestAction); await requestDelegate(httpContext); @@ -414,7 +414,7 @@ void TestAction([FromBody] Todo todo) httpContext.Features.Set(new TestHttpRequestLifetimeFeature()); httpContext.RequestServices = serviceCollection.BuildServiceProvider(); - var requestDelegate = MapActionExpressionTreeBuilder.BuildRequestDelegate((Action)TestAction); + var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate((Action)TestAction); await requestDelegate(httpContext); @@ -450,7 +450,7 @@ void TestAction([FromBody] Todo todo) httpContext.Features.Set(new TestHttpRequestLifetimeFeature()); httpContext.RequestServices = serviceCollection.BuildServiceProvider(); - var requestDelegate = MapActionExpressionTreeBuilder.BuildRequestDelegate((Action)TestAction); + var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate((Action)TestAction); await requestDelegate(httpContext); @@ -485,7 +485,7 @@ void TestAction([FromForm] int value) var httpContext = new DefaultHttpContext(); httpContext.Request.Form = form; - var requestDelegate = MapActionExpressionTreeBuilder.BuildRequestDelegate((Action)TestAction); + var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate((Action)TestAction); await requestDelegate(httpContext); @@ -515,7 +515,7 @@ void TestAction([FromForm] int value) httpContext.Features.Set(new TestHttpRequestLifetimeFeature()); httpContext.RequestServices = serviceCollection.BuildServiceProvider(); - var requestDelegate = MapActionExpressionTreeBuilder.BuildRequestDelegate((Action)TestAction); + var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate((Action)TestAction); await requestDelegate(httpContext); @@ -551,7 +551,7 @@ void TestAction([FromForm] int value) httpContext.Features.Set(new TestHttpRequestLifetimeFeature()); httpContext.RequestServices = serviceCollection.BuildServiceProvider(); - var requestDelegate = MapActionExpressionTreeBuilder.BuildRequestDelegate((Action)TestAction); + var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate((Action)TestAction); await requestDelegate(httpContext); @@ -571,8 +571,8 @@ public void BuildRequestDelegateThrowsInvalidOperationExceptionGivenBothFromBody void TestAction([FromBody] int value1, [FromForm] int value2) { } void TestActionWithFlippedParams([FromForm] int value1, [FromBody] int value2) { } - Assert.Throws(() => MapActionExpressionTreeBuilder.BuildRequestDelegate((Action)TestAction)); - Assert.Throws(() => MapActionExpressionTreeBuilder.BuildRequestDelegate((Action)TestActionWithFlippedParams)); + Assert.Throws(() => RequestDelegateBuilder.BuildRequestDelegate((Action)TestAction)); + Assert.Throws(() => RequestDelegateBuilder.BuildRequestDelegate((Action)TestActionWithFlippedParams)); } [Fact] @@ -580,7 +580,7 @@ public void BuildRequestDelegateThrowsInvalidOperationExceptionGivenFromBodyOnMu { void TestAction([FromBody] int value1, [FromBody] int value2) { } - Assert.Throws(() => MapActionExpressionTreeBuilder.BuildRequestDelegate((Action)TestAction)); + Assert.Throws(() => RequestDelegateBuilder.BuildRequestDelegate((Action)TestAction)); } [Fact] @@ -600,7 +600,7 @@ void TestAction([FromService] MyService myService) var httpContext = new DefaultHttpContext(); httpContext.RequestServices = serviceCollection.BuildServiceProvider(); - var requestDelegate = MapActionExpressionTreeBuilder.BuildRequestDelegate((Action)TestAction); + var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate((Action)TestAction); await requestDelegate(httpContext); @@ -619,7 +619,7 @@ void TestAction(HttpContext httpContext) var httpContext = new DefaultHttpContext(); - var requestDelegate = MapActionExpressionTreeBuilder.BuildRequestDelegate((Action)TestAction); + var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate((Action)TestAction); await requestDelegate(httpContext); @@ -639,7 +639,7 @@ void TestAction(IFormCollection httpContext) var httpContext = new DefaultHttpContext(); httpContext.Request.Headers["Content-Type"] = "application/x-www-form-urlencoded"; - var requestDelegate = MapActionExpressionTreeBuilder.BuildRequestDelegate((Action)TestAction); + var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate((Action)TestAction); await requestDelegate(httpContext); @@ -662,7 +662,7 @@ void TestAction(CancellationToken cancellationToken) RequestAborted = cts.Token }; - var requestDelegate = MapActionExpressionTreeBuilder.BuildRequestDelegate((Action)TestAction); + var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate((Action)TestAction); await requestDelegate(httpContext); @@ -706,7 +706,7 @@ public async Task RequestDelegateWritesComplexReturnValueAsJsonResponseBody(Dele var responseBodyStream = new MemoryStream(); httpContext.Response.Body = responseBodyStream; - var requestDelegate = MapActionExpressionTreeBuilder.BuildRequestDelegate(@delegate); + var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate(@delegate); await requestDelegate(httpContext); @@ -755,7 +755,7 @@ public async Task RequestDelegateUsesCustomIResult(Delegate @delegate) var responseBodyStream = new MemoryStream(); httpContext.Response.Body = responseBodyStream; - var requestDelegate = MapActionExpressionTreeBuilder.BuildRequestDelegate(@delegate); + var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate(@delegate); await requestDelegate(httpContext); @@ -798,7 +798,7 @@ public async Task RequestDelegateWritesStringReturnValueAsJsonResponseBody(Deleg var responseBodyStream = new MemoryStream(); httpContext.Response.Body = responseBodyStream; - var requestDelegate = MapActionExpressionTreeBuilder.BuildRequestDelegate(@delegate); + var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate(@delegate); await requestDelegate(httpContext); @@ -839,7 +839,7 @@ public async Task RequestDelegateWritesIntReturnValue(Delegate @delegate) var responseBodyStream = new MemoryStream(); httpContext.Response.Body = responseBodyStream; - var requestDelegate = MapActionExpressionTreeBuilder.BuildRequestDelegate(@delegate); + var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate(@delegate); await requestDelegate(httpContext); @@ -880,7 +880,7 @@ public async Task RequestDelegateWritesBoolReturnValue(Delegate @delegate) var responseBodyStream = new MemoryStream(); httpContext.Response.Body = responseBodyStream; - var requestDelegate = MapActionExpressionTreeBuilder.BuildRequestDelegate(@delegate); + var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate(@delegate); await requestDelegate(httpContext); diff --git a/src/Http/Routing/src/Builder/MapActionEndpointRouteBuilderExtensions.cs b/src/Http/Routing/src/Builder/MapActionEndpointRouteBuilderExtensions.cs index e816c1cdb779..ac412bcdc689 100644 --- a/src/Http/Routing/src/Builder/MapActionEndpointRouteBuilderExtensions.cs +++ b/src/Http/Routing/src/Builder/MapActionEndpointRouteBuilderExtensions.cs @@ -5,8 +5,8 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Routing; -using Microsoft.AspNetCore.Routing.Internal; using Microsoft.AspNetCore.Routing.Patterns; namespace Microsoft.AspNetCore.Builder @@ -159,7 +159,7 @@ public static MapActionEndpointConventionBuilder Map( const int defaultOrder = 0; var builder = new RouteEndpointBuilder( - MapActionExpressionTreeBuilder.BuildRequestDelegate(action), + RequestDelegateBuilder.BuildRequestDelegate(action), pattern, defaultOrder) { diff --git a/src/Http/Routing/src/Microsoft.AspNetCore.Routing.csproj b/src/Http/Routing/src/Microsoft.AspNetCore.Routing.csproj index 67723bd1d175..e96f32ceddf5 100644 --- a/src/Http/Routing/src/Microsoft.AspNetCore.Routing.csproj +++ b/src/Http/Routing/src/Microsoft.AspNetCore.Routing.csproj @@ -24,7 +24,6 @@ Microsoft.AspNetCore.Routing.RouteCollection - From 6921e0a6e701e6bc4682beb5288b44b84645c7ac Mon Sep 17 00:00:00 2001 From: Stephen Halter Date: Wed, 24 Mar 2021 22:23:28 -0700 Subject: [PATCH 2/6] Add new BuildRequestDelegate overloads --- .../src/PublicAPI.Unshipped.txt | 2 + .../src/RequestDelegateBuilder.cs | 118 ++++++++++++++---- ...ests.cs => RequestDelegateBuilderTests.cs} | 95 +++++++++++++- 3 files changed, 189 insertions(+), 26 deletions(-) rename src/Http/Http.Extensions/test/{RouteDelegateBuilderTests.cs => RequestDelegateBuilderTests.cs} (91%) diff --git a/src/Http/Http.Extensions/src/PublicAPI.Unshipped.txt b/src/Http/Http.Extensions/src/PublicAPI.Unshipped.txt index abdd44314e49..800d4fcc4d9b 100644 --- a/src/Http/Http.Extensions/src/PublicAPI.Unshipped.txt +++ b/src/Http/Http.Extensions/src/PublicAPI.Unshipped.txt @@ -170,6 +170,8 @@ static Microsoft.AspNetCore.Http.HeaderDictionaryTypeExtensions.GetTypedHeaders( static Microsoft.AspNetCore.Http.HeaderDictionaryTypeExtensions.GetTypedHeaders(this Microsoft.AspNetCore.Http.HttpResponse! response) -> Microsoft.AspNetCore.Http.Headers.ResponseHeaders! static Microsoft.AspNetCore.Http.HttpContextServerVariableExtensions.GetServerVariable(this Microsoft.AspNetCore.Http.HttpContext! context, string! variableName) -> string? static Microsoft.AspNetCore.Http.RequestDelegateBuilder.BuildRequestDelegate(System.Delegate! action) -> Microsoft.AspNetCore.Http.RequestDelegate! +static Microsoft.AspNetCore.Http.RequestDelegateBuilder.BuildRequestDelegate(System.Reflection.MethodInfo! methodInfo) -> Microsoft.AspNetCore.Http.RequestDelegate! +static Microsoft.AspNetCore.Http.RequestDelegateBuilder.BuildRequestDelegate(System.Reflection.MethodInfo! methodInfo, System.Func! targetFactory) -> Microsoft.AspNetCore.Http.RequestDelegate! static Microsoft.AspNetCore.Http.ResponseExtensions.Clear(this Microsoft.AspNetCore.Http.HttpResponse! response) -> void static Microsoft.AspNetCore.Http.ResponseExtensions.Redirect(this Microsoft.AspNetCore.Http.HttpResponse! response, string! location, bool permanent, bool preserveMethod) -> void static Microsoft.AspNetCore.Http.SendFileResponseExtensions.SendFileAsync(this Microsoft.AspNetCore.Http.HttpResponse! response, Microsoft.Extensions.FileProviders.IFileInfo! file, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task! diff --git a/src/Http/Http.Extensions/src/RequestDelegateBuilder.cs b/src/Http/Http.Extensions/src/RequestDelegateBuilder.cs index 2f1d35f5e0c6..8ef096ce579a 100644 --- a/src/Http/Http.Extensions/src/RequestDelegateBuilder.cs +++ b/src/Http/Http.Extensions/src/RequestDelegateBuilder.cs @@ -52,6 +52,80 @@ public static class RequestDelegateBuilder /// A request handler with any number of custom parameters that often produces a response with its return value. /// The public static RequestDelegate BuildRequestDelegate(Delegate action) + { + if (action is null) + { + throw new ArgumentNullException(nameof(action)); + } + + var targetExpression = action.Target switch + { + { } => Expression.Convert(TargetArg, action.Target.GetType()), + null => null, + }; + + var untargetedRequestDelegate = BuildRequestDelegate(action.Method, targetExpression); + + return httpContext => + { + return untargetedRequestDelegate(action.Target, httpContext); + }; + } + + /// + /// Builds a implementation for . + /// + /// A static request handler with any number of custom parameters that often produces a response with its return value. + /// The + public static RequestDelegate BuildRequestDelegate(MethodInfo methodInfo) + { + if (methodInfo is null) + { + throw new ArgumentNullException(nameof(methodInfo)); + } + + var untargetedRequestDelegate = BuildRequestDelegate(methodInfo, targetExpression: null); + + return httpContext => + { + return untargetedRequestDelegate(null, httpContext); + }; + } + + + /// + /// Builds a implementation for . + /// + /// A request handler with any number of custom parameters that often produces a response with its return value. + /// Creates the for the non-static method. If the + /// The + public static RequestDelegate BuildRequestDelegate(MethodInfo methodInfo, Func targetFactory) + { + if (methodInfo is null) + { + throw new ArgumentNullException(nameof(methodInfo)); + } + + if (targetFactory is null) + { + throw new ArgumentNullException(nameof(targetFactory)); + } + + if (methodInfo.DeclaringType is null) + { + throw new ArgumentException($"A {nameof(targetFactory)} was provided, but {nameof(methodInfo)} does not have a Declaring type."); + } + + var targetExpression = Expression.Convert(TargetArg, methodInfo.DeclaringType); + var untargetedRequestDelegate = BuildRequestDelegate(methodInfo, targetExpression); + + return httpContext => + { + return untargetedRequestDelegate(targetFactory(httpContext), httpContext); + }; + } + + private static Func BuildRequestDelegate(MethodInfo methodInfo, Expression? targetExpression) { // Non void return type @@ -69,8 +143,6 @@ public static RequestDelegate BuildRequestDelegate(Delegate action) // return default; // } - var method = action.Method; - var consumeBodyDirectly = false; var consumeBodyAsForm = false; Type? bodyType = null; @@ -79,7 +151,7 @@ public static RequestDelegate BuildRequestDelegate(Delegate action) // This argument represents the deserialized body returned from IHttpRequestReader // when the method has a FromBody attribute declared - var methodParameters = method.GetParameters(); + var methodParameters = methodInfo.GetParameters(); var args = new List(methodParameters.Length); foreach (var parameter in methodParameters) @@ -163,18 +235,17 @@ public static RequestDelegate BuildRequestDelegate(Delegate action) MethodCallExpression methodCall; - if (action.Target is null) + if (targetExpression is null) { - methodCall = Expression.Call(method, args); + methodCall = Expression.Call(methodInfo, args); } else { - var castedTarget = Expression.Convert(TargetArg, action.Target.GetType()); - methodCall = Expression.Call(castedTarget, method, args); + methodCall = Expression.Call(targetExpression, methodInfo, args); } // Exact request delegate match - if (method.ReturnType == typeof(void)) + if (methodInfo.ReturnType == typeof(void)) { var bodyExpressions = new List { @@ -184,22 +255,22 @@ public static RequestDelegate BuildRequestDelegate(Delegate action) body = Expression.Block(bodyExpressions); } - else if (AwaitableInfo.IsTypeAwaitable(method.ReturnType, out var info)) + else if (AwaitableInfo.IsTypeAwaitable(methodInfo.ReturnType, out var info)) { - if (method.ReturnType == typeof(Task)) + if (methodInfo.ReturnType == typeof(Task)) { body = methodCall; } - else if (method.ReturnType == typeof(ValueTask)) + else if (methodInfo.ReturnType == typeof(ValueTask)) { body = Expression.Call( ExecuteValueTaskMethodInfo, methodCall); } - else if (method.ReturnType.IsGenericType && - method.ReturnType.GetGenericTypeDefinition() == typeof(Task<>)) + else if (methodInfo.ReturnType.IsGenericType && + methodInfo.ReturnType.GetGenericTypeDefinition() == typeof(Task<>)) { - var typeArg = method.ReturnType.GetGenericArguments()[0]; + var typeArg = methodInfo.ReturnType.GetGenericArguments()[0]; if (typeof(IResult).IsAssignableFrom(typeArg)) { @@ -227,10 +298,10 @@ public static RequestDelegate BuildRequestDelegate(Delegate action) } } } - else if (method.ReturnType.IsGenericType && - method.ReturnType.GetGenericTypeDefinition() == typeof(ValueTask<>)) + else if (methodInfo.ReturnType.IsGenericType && + methodInfo.ReturnType.GetGenericTypeDefinition() == typeof(ValueTask<>)) { - var typeArg = method.ReturnType.GetGenericArguments()[0]; + var typeArg = methodInfo.ReturnType.GetGenericArguments()[0]; if (typeof(IResult).IsAssignableFrom(typeArg)) { @@ -261,18 +332,18 @@ public static RequestDelegate BuildRequestDelegate(Delegate action) else { // TODO: Handle custom awaitables - throw new NotSupportedException($"Unsupported return type: {method.ReturnType}"); + throw new NotSupportedException($"Unsupported return type: {methodInfo.ReturnType}"); } } - else if (typeof(IResult).IsAssignableFrom(method.ReturnType)) + else if (typeof(IResult).IsAssignableFrom(methodInfo.ReturnType)) { body = Expression.Call(methodCall, ResultWriteResponseAsync, HttpContextParameter); } - else if (method.ReturnType == typeof(string)) + else if (methodInfo.ReturnType == typeof(string)) { body = Expression.Call(StringResultWriteResponseAsync, HttpResponseExpr, methodCall, Expression.Constant(CancellationToken.None)); } - else if (method.ReturnType.IsValueType) + else if (methodInfo.ReturnType.IsValueType) { var box = Expression.TypeAs(methodCall, typeof(object)); body = Expression.Call(JsonResultWriteResponseAsync, HttpResponseExpr, box, Expression.Constant(CancellationToken.None)); @@ -364,10 +435,7 @@ public static RequestDelegate BuildRequestDelegate(Delegate action) requestDelegate = invoker; } - return httpContext => - { - return requestDelegate(action.Target, httpContext); - }; + return requestDelegate; } private static ILogger GetLogger(HttpContext httpContext) diff --git a/src/Http/Http.Extensions/test/RouteDelegateBuilderTests.cs b/src/Http/Http.Extensions/test/RequestDelegateBuilderTests.cs similarity index 91% rename from src/Http/Http.Extensions/test/RouteDelegateBuilderTests.cs rename to src/Http/Http.Extensions/test/RequestDelegateBuilderTests.cs index 4e0b65c781da..3e3a6b7cdce1 100644 --- a/src/Http/Http.Extensions/test/RouteDelegateBuilderTests.cs +++ b/src/Http/Http.Extensions/test/RequestDelegateBuilderTests.cs @@ -7,6 +7,7 @@ using System.Collections.Generic; using System.Globalization; using System.IO; +using System.Reflection; using System.Text; using System.Text.Json; using System.Threading; @@ -22,7 +23,7 @@ namespace Microsoft.AspNetCore.Routing.Internal { - public class RouteDelegateBuilderTests + public class RequestDelegateBuilderTests { public static IEnumerable NoResult { @@ -92,6 +93,98 @@ public async Task RequestDelegateInvokesAction(Delegate @delegate) Assert.True(httpContext.Items["invoked"] as bool?); } + private static void StaticTestActionBasicReflection(HttpContext httpContext) + { + httpContext.Items.Add("invoked", true); + } + + [Fact] + public async Task StaticMethodInfoOverloadWorksWithBasicReflection() + { + var methodInfo = typeof(RequestDelegateBuilderTests).GetMethod( + nameof(StaticTestActionBasicReflection), + BindingFlags.NonPublic | BindingFlags.Static, + new[] { typeof(HttpContext) }); + + var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate(methodInfo!); + + var httpContext = new DefaultHttpContext(); + + await requestDelegate(httpContext); + + Assert.True(httpContext.Items["invoked"] as bool?); + } + + private class TestNonStaticActionClass + { + private readonly object _invokedValue; + + public TestNonStaticActionClass(object invokedValue) + { + _invokedValue = invokedValue; + } + + private void NonStaticTestAction(HttpContext httpContext) + { + httpContext.Items.Add("invoked", _invokedValue); + } + } + + [Fact] + public async Task NonStaticMethodInfoOverloadWorksWithBasicReflection() + { + var methodInfo = typeof(TestNonStaticActionClass).GetMethod( + "NonStaticTestAction", + BindingFlags.NonPublic | BindingFlags.Instance, + new[] { typeof(HttpContext) }); + + var invoked = false; + + object GetTarget() + { + if (!invoked) + { + invoked = true; + return new TestNonStaticActionClass(1); + } + + return new TestNonStaticActionClass(2); + } + + var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate(methodInfo!, _ => GetTarget()); + + var httpContext = new DefaultHttpContext(); + + await requestDelegate(httpContext); + + Assert.Equal(1, httpContext.Items["invoked"]); + + httpContext = new DefaultHttpContext(); + + await requestDelegate(httpContext); + + Assert.Equal(2, httpContext.Items["invoked"]); + } + + [Fact] + public void BuildRequestDelegateThrowsArgumentNullExceptions() + { + var methodInfo = typeof(RequestDelegateBuilderTests).GetMethod( + nameof(StaticTestActionBasicReflection), + BindingFlags.NonPublic | BindingFlags.Static, + new[] { typeof(HttpContext) }); + + var exNullAction = Assert.Throws(() => RequestDelegateBuilder.BuildRequestDelegate(action: null!)); + var exNullMethodInfo1 = Assert.Throws(() => RequestDelegateBuilder.BuildRequestDelegate(methodInfo: null!)); + var exNullMethodInfo2 = Assert.Throws(() => RequestDelegateBuilder.BuildRequestDelegate(methodInfo: null!, _ => 0)); + var exNullTargetFactory = Assert.Throws(() => RequestDelegateBuilder.BuildRequestDelegate(methodInfo!, targetFactory: null!)); + + Assert.Equal("action", exNullAction.ParamName); + Assert.Equal("methodInfo", exNullMethodInfo1.ParamName); + Assert.Equal("methodInfo", exNullMethodInfo2.ParamName); + Assert.Equal("targetFactory", exNullTargetFactory.ParamName); + } + public static IEnumerable FromRouteResult { get From 88b469af44dba0745b2c626ddb19755c764e4a1c Mon Sep 17 00:00:00 2001 From: Stephen Halter Date: Thu, 25 Mar 2021 19:28:41 -0700 Subject: [PATCH 3/6] Address PR feedback --- .../src/PublicAPI.Unshipped.txt | 6 +- .../src/RequestDelegateBuilder.cs | 9 ++- .../test/RequestDelegateBuilderTests.cs | 70 +++++++++---------- ...MapActionEndpointRouteBuilderExtensions.cs | 2 +- 4 files changed, 43 insertions(+), 44 deletions(-) diff --git a/src/Http/Http.Extensions/src/PublicAPI.Unshipped.txt b/src/Http/Http.Extensions/src/PublicAPI.Unshipped.txt index 800d4fcc4d9b..d96bc0f11a95 100644 --- a/src/Http/Http.Extensions/src/PublicAPI.Unshipped.txt +++ b/src/Http/Http.Extensions/src/PublicAPI.Unshipped.txt @@ -169,9 +169,9 @@ static Microsoft.AspNetCore.Http.HeaderDictionaryTypeExtensions.AppendList(th static Microsoft.AspNetCore.Http.HeaderDictionaryTypeExtensions.GetTypedHeaders(this Microsoft.AspNetCore.Http.HttpRequest! request) -> Microsoft.AspNetCore.Http.Headers.RequestHeaders! static Microsoft.AspNetCore.Http.HeaderDictionaryTypeExtensions.GetTypedHeaders(this Microsoft.AspNetCore.Http.HttpResponse! response) -> Microsoft.AspNetCore.Http.Headers.ResponseHeaders! static Microsoft.AspNetCore.Http.HttpContextServerVariableExtensions.GetServerVariable(this Microsoft.AspNetCore.Http.HttpContext! context, string! variableName) -> string? -static Microsoft.AspNetCore.Http.RequestDelegateBuilder.BuildRequestDelegate(System.Delegate! action) -> Microsoft.AspNetCore.Http.RequestDelegate! -static Microsoft.AspNetCore.Http.RequestDelegateBuilder.BuildRequestDelegate(System.Reflection.MethodInfo! methodInfo) -> Microsoft.AspNetCore.Http.RequestDelegate! -static Microsoft.AspNetCore.Http.RequestDelegateBuilder.BuildRequestDelegate(System.Reflection.MethodInfo! methodInfo, System.Func! targetFactory) -> Microsoft.AspNetCore.Http.RequestDelegate! +static Microsoft.AspNetCore.Http.RequestDelegateBuilder.Build(System.Delegate! action) -> Microsoft.AspNetCore.Http.RequestDelegate! +static Microsoft.AspNetCore.Http.RequestDelegateBuilder.Build(System.Reflection.MethodInfo! methodInfo) -> Microsoft.AspNetCore.Http.RequestDelegate! +static Microsoft.AspNetCore.Http.RequestDelegateBuilder.Build(System.Reflection.MethodInfo! methodInfo, System.Func! targetFactory) -> Microsoft.AspNetCore.Http.RequestDelegate! static Microsoft.AspNetCore.Http.ResponseExtensions.Clear(this Microsoft.AspNetCore.Http.HttpResponse! response) -> void static Microsoft.AspNetCore.Http.ResponseExtensions.Redirect(this Microsoft.AspNetCore.Http.HttpResponse! response, string! location, bool permanent, bool preserveMethod) -> void static Microsoft.AspNetCore.Http.SendFileResponseExtensions.SendFileAsync(this Microsoft.AspNetCore.Http.HttpResponse! response, Microsoft.Extensions.FileProviders.IFileInfo! file, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task! diff --git a/src/Http/Http.Extensions/src/RequestDelegateBuilder.cs b/src/Http/Http.Extensions/src/RequestDelegateBuilder.cs index 8ef096ce579a..ff410e865674 100644 --- a/src/Http/Http.Extensions/src/RequestDelegateBuilder.cs +++ b/src/Http/Http.Extensions/src/RequestDelegateBuilder.cs @@ -51,7 +51,7 @@ public static class RequestDelegateBuilder /// /// A request handler with any number of custom parameters that often produces a response with its return value. /// The - public static RequestDelegate BuildRequestDelegate(Delegate action) + public static RequestDelegate Build(Delegate action) { if (action is null) { @@ -77,7 +77,7 @@ public static RequestDelegate BuildRequestDelegate(Delegate action) /// /// A static request handler with any number of custom parameters that often produces a response with its return value. /// The - public static RequestDelegate BuildRequestDelegate(MethodInfo methodInfo) + public static RequestDelegate Build(MethodInfo methodInfo) { if (methodInfo is null) { @@ -92,14 +92,13 @@ public static RequestDelegate BuildRequestDelegate(MethodInfo methodInfo) }; } - /// /// Builds a implementation for . /// /// A request handler with any number of custom parameters that often produces a response with its return value. - /// Creates the for the non-static method. If the + /// Creates the for the non-static method. /// The - public static RequestDelegate BuildRequestDelegate(MethodInfo methodInfo, Func targetFactory) + public static RequestDelegate Build(MethodInfo methodInfo, Func targetFactory) { if (methodInfo is null) { diff --git a/src/Http/Http.Extensions/test/RequestDelegateBuilderTests.cs b/src/Http/Http.Extensions/test/RequestDelegateBuilderTests.cs index 3e3a6b7cdce1..a5eb6d981d2e 100644 --- a/src/Http/Http.Extensions/test/RequestDelegateBuilderTests.cs +++ b/src/Http/Http.Extensions/test/RequestDelegateBuilderTests.cs @@ -86,7 +86,7 @@ public async Task RequestDelegateInvokesAction(Delegate @delegate) { var httpContext = new DefaultHttpContext(); - var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate(@delegate); + var requestDelegate = RequestDelegateBuilder.Build(@delegate); await requestDelegate(httpContext); @@ -106,7 +106,7 @@ public async Task StaticMethodInfoOverloadWorksWithBasicReflection() BindingFlags.NonPublic | BindingFlags.Static, new[] { typeof(HttpContext) }); - var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate(methodInfo!); + var requestDelegate = RequestDelegateBuilder.Build(methodInfo!); var httpContext = new DefaultHttpContext(); @@ -151,7 +151,7 @@ object GetTarget() return new TestNonStaticActionClass(2); } - var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate(methodInfo!, _ => GetTarget()); + var requestDelegate = RequestDelegateBuilder.Build(methodInfo!, _ => GetTarget()); var httpContext = new DefaultHttpContext(); @@ -174,10 +174,10 @@ public void BuildRequestDelegateThrowsArgumentNullExceptions() BindingFlags.NonPublic | BindingFlags.Static, new[] { typeof(HttpContext) }); - var exNullAction = Assert.Throws(() => RequestDelegateBuilder.BuildRequestDelegate(action: null!)); - var exNullMethodInfo1 = Assert.Throws(() => RequestDelegateBuilder.BuildRequestDelegate(methodInfo: null!)); - var exNullMethodInfo2 = Assert.Throws(() => RequestDelegateBuilder.BuildRequestDelegate(methodInfo: null!, _ => 0)); - var exNullTargetFactory = Assert.Throws(() => RequestDelegateBuilder.BuildRequestDelegate(methodInfo!, targetFactory: null!)); + var exNullAction = Assert.Throws(() => RequestDelegateBuilder.Build(action: null!)); + var exNullMethodInfo1 = Assert.Throws(() => RequestDelegateBuilder.Build(methodInfo: null!)); + var exNullMethodInfo2 = Assert.Throws(() => RequestDelegateBuilder.Build(methodInfo: null!, _ => 0)); + var exNullTargetFactory = Assert.Throws(() => RequestDelegateBuilder.Build(methodInfo!, targetFactory: null!)); Assert.Equal("action", exNullAction.ParamName); Assert.Equal("methodInfo", exNullMethodInfo1.ParamName); @@ -229,7 +229,7 @@ public async Task RequestDelegatePopulatesFromRouteParameterBasedOnParameterName var httpContext = new DefaultHttpContext(); httpContext.Request.RouteValues[paramName] = originalRouteParam.ToString(NumberFormatInfo.InvariantInfo); - var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate(@delegate); + var requestDelegate = RequestDelegateBuilder.Build(@delegate); await requestDelegate(httpContext); @@ -272,7 +272,7 @@ public async Task RequestDelegatePopulatesFromRouteOptionalParameter(Delegate @d { var httpContext = new DefaultHttpContext(); - var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate(@delegate); + var requestDelegate = RequestDelegateBuilder.Build(@delegate); await requestDelegate(httpContext); @@ -290,7 +290,7 @@ public async Task RequestDelegatePopulatesFromRouteOptionalParameterBasedOnParam httpContext.Request.RouteValues[paramName] = originalRouteParam.ToString(NumberFormatInfo.InvariantInfo); - var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate(@delegate); + var requestDelegate = RequestDelegateBuilder.Build(@delegate); await requestDelegate(httpContext); @@ -313,7 +313,7 @@ void TestAction([FromRoute(Name = specifiedName)] int foo) var httpContext = new DefaultHttpContext(); httpContext.Request.RouteValues[specifiedName] = originalRouteParam.ToString(NumberFormatInfo.InvariantInfo); - var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate((Action)TestAction); + var requestDelegate = RequestDelegateBuilder.Build((Action)TestAction); await requestDelegate(httpContext); @@ -336,7 +336,7 @@ void TestAction([FromRoute] int foo) var httpContext = new DefaultHttpContext(); httpContext.Request.RouteValues[unmatchedName] = unmatchedRouteParam.ToString(NumberFormatInfo.InvariantInfo); - var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate((Action)TestAction); + var requestDelegate = RequestDelegateBuilder.Build((Action)TestAction); await requestDelegate(httpContext); @@ -364,7 +364,7 @@ void TestAction([FromQuery] int value) var httpContext = new DefaultHttpContext(); httpContext.Request.Query = query; - var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate((Action)TestAction); + var requestDelegate = RequestDelegateBuilder.Build((Action)TestAction); await requestDelegate(httpContext); @@ -387,7 +387,7 @@ void TestAction([FromHeader(Name = customHeaderName)] int value) var httpContext = new DefaultHttpContext(); httpContext.Request.Headers[customHeaderName] = originalHeaderParam.ToString(NumberFormatInfo.InvariantInfo); - var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate((Action)TestAction); + var requestDelegate = RequestDelegateBuilder.Build((Action)TestAction); await requestDelegate(httpContext); @@ -415,7 +415,7 @@ void TestAction([FromBody] Todo todo) var requestBodyBytes = JsonSerializer.SerializeToUtf8Bytes(originalTodo); httpContext.Request.Body = new MemoryStream(requestBodyBytes); - var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate((Action)TestAction); + var requestDelegate = RequestDelegateBuilder.Build((Action)TestAction); await requestDelegate(httpContext); @@ -434,7 +434,7 @@ void TestAction([FromBody] Todo todo) httpContext.Request.Headers["Content-Type"] = "application/json"; httpContext.Request.Headers["Content-Length"] = "0"; - var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate((Action)TestAction); + var requestDelegate = RequestDelegateBuilder.Build((Action)TestAction); await Assert.ThrowsAsync(() => requestDelegate(httpContext)); } @@ -453,7 +453,7 @@ void TestAction([FromBody(AllowEmpty = true)] Todo todo) httpContext.Request.Headers["Content-Type"] = "application/json"; httpContext.Request.Headers["Content-Length"] = "0"; - var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate((Action)TestAction); + var requestDelegate = RequestDelegateBuilder.Build((Action)TestAction); await requestDelegate(httpContext); @@ -477,7 +477,7 @@ void TestAction([FromBody(AllowEmpty = true)] BodyStruct bodyStruct) httpContext.Request.Headers["Content-Type"] = "application/json"; httpContext.Request.Headers["Content-Length"] = "0"; - var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate((Action)TestAction); + var requestDelegate = RequestDelegateBuilder.Build((Action)TestAction); await requestDelegate(httpContext); @@ -507,7 +507,7 @@ void TestAction([FromBody] Todo todo) httpContext.Features.Set(new TestHttpRequestLifetimeFeature()); httpContext.RequestServices = serviceCollection.BuildServiceProvider(); - var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate((Action)TestAction); + var requestDelegate = RequestDelegateBuilder.Build((Action)TestAction); await requestDelegate(httpContext); @@ -543,7 +543,7 @@ void TestAction([FromBody] Todo todo) httpContext.Features.Set(new TestHttpRequestLifetimeFeature()); httpContext.RequestServices = serviceCollection.BuildServiceProvider(); - var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate((Action)TestAction); + var requestDelegate = RequestDelegateBuilder.Build((Action)TestAction); await requestDelegate(httpContext); @@ -578,7 +578,7 @@ void TestAction([FromForm] int value) var httpContext = new DefaultHttpContext(); httpContext.Request.Form = form; - var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate((Action)TestAction); + var requestDelegate = RequestDelegateBuilder.Build((Action)TestAction); await requestDelegate(httpContext); @@ -608,7 +608,7 @@ void TestAction([FromForm] int value) httpContext.Features.Set(new TestHttpRequestLifetimeFeature()); httpContext.RequestServices = serviceCollection.BuildServiceProvider(); - var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate((Action)TestAction); + var requestDelegate = RequestDelegateBuilder.Build((Action)TestAction); await requestDelegate(httpContext); @@ -644,7 +644,7 @@ void TestAction([FromForm] int value) httpContext.Features.Set(new TestHttpRequestLifetimeFeature()); httpContext.RequestServices = serviceCollection.BuildServiceProvider(); - var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate((Action)TestAction); + var requestDelegate = RequestDelegateBuilder.Build((Action)TestAction); await requestDelegate(httpContext); @@ -664,8 +664,8 @@ public void BuildRequestDelegateThrowsInvalidOperationExceptionGivenBothFromBody void TestAction([FromBody] int value1, [FromForm] int value2) { } void TestActionWithFlippedParams([FromForm] int value1, [FromBody] int value2) { } - Assert.Throws(() => RequestDelegateBuilder.BuildRequestDelegate((Action)TestAction)); - Assert.Throws(() => RequestDelegateBuilder.BuildRequestDelegate((Action)TestActionWithFlippedParams)); + Assert.Throws(() => RequestDelegateBuilder.Build((Action)TestAction)); + Assert.Throws(() => RequestDelegateBuilder.Build((Action)TestActionWithFlippedParams)); } [Fact] @@ -673,7 +673,7 @@ public void BuildRequestDelegateThrowsInvalidOperationExceptionGivenFromBodyOnMu { void TestAction([FromBody] int value1, [FromBody] int value2) { } - Assert.Throws(() => RequestDelegateBuilder.BuildRequestDelegate((Action)TestAction)); + Assert.Throws(() => RequestDelegateBuilder.Build((Action)TestAction)); } [Fact] @@ -693,7 +693,7 @@ void TestAction([FromService] MyService myService) var httpContext = new DefaultHttpContext(); httpContext.RequestServices = serviceCollection.BuildServiceProvider(); - var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate((Action)TestAction); + var requestDelegate = RequestDelegateBuilder.Build((Action)TestAction); await requestDelegate(httpContext); @@ -712,7 +712,7 @@ void TestAction(HttpContext httpContext) var httpContext = new DefaultHttpContext(); - var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate((Action)TestAction); + var requestDelegate = RequestDelegateBuilder.Build((Action)TestAction); await requestDelegate(httpContext); @@ -732,7 +732,7 @@ void TestAction(IFormCollection httpContext) var httpContext = new DefaultHttpContext(); httpContext.Request.Headers["Content-Type"] = "application/x-www-form-urlencoded"; - var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate((Action)TestAction); + var requestDelegate = RequestDelegateBuilder.Build((Action)TestAction); await requestDelegate(httpContext); @@ -755,7 +755,7 @@ void TestAction(CancellationToken cancellationToken) RequestAborted = cts.Token }; - var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate((Action)TestAction); + var requestDelegate = RequestDelegateBuilder.Build((Action)TestAction); await requestDelegate(httpContext); @@ -799,7 +799,7 @@ public async Task RequestDelegateWritesComplexReturnValueAsJsonResponseBody(Dele var responseBodyStream = new MemoryStream(); httpContext.Response.Body = responseBodyStream; - var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate(@delegate); + var requestDelegate = RequestDelegateBuilder.Build(@delegate); await requestDelegate(httpContext); @@ -848,7 +848,7 @@ public async Task RequestDelegateUsesCustomIResult(Delegate @delegate) var responseBodyStream = new MemoryStream(); httpContext.Response.Body = responseBodyStream; - var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate(@delegate); + var requestDelegate = RequestDelegateBuilder.Build(@delegate); await requestDelegate(httpContext); @@ -891,7 +891,7 @@ public async Task RequestDelegateWritesStringReturnValueAsJsonResponseBody(Deleg var responseBodyStream = new MemoryStream(); httpContext.Response.Body = responseBodyStream; - var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate(@delegate); + var requestDelegate = RequestDelegateBuilder.Build(@delegate); await requestDelegate(httpContext); @@ -932,7 +932,7 @@ public async Task RequestDelegateWritesIntReturnValue(Delegate @delegate) var responseBodyStream = new MemoryStream(); httpContext.Response.Body = responseBodyStream; - var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate(@delegate); + var requestDelegate = RequestDelegateBuilder.Build(@delegate); await requestDelegate(httpContext); @@ -973,7 +973,7 @@ public async Task RequestDelegateWritesBoolReturnValue(Delegate @delegate) var responseBodyStream = new MemoryStream(); httpContext.Response.Body = responseBodyStream; - var requestDelegate = RequestDelegateBuilder.BuildRequestDelegate(@delegate); + var requestDelegate = RequestDelegateBuilder.Build(@delegate); await requestDelegate(httpContext); diff --git a/src/Http/Routing/src/Builder/MapActionEndpointRouteBuilderExtensions.cs b/src/Http/Routing/src/Builder/MapActionEndpointRouteBuilderExtensions.cs index ac412bcdc689..0205ebdcd784 100644 --- a/src/Http/Routing/src/Builder/MapActionEndpointRouteBuilderExtensions.cs +++ b/src/Http/Routing/src/Builder/MapActionEndpointRouteBuilderExtensions.cs @@ -159,7 +159,7 @@ public static MapActionEndpointConventionBuilder Map( const int defaultOrder = 0; var builder = new RouteEndpointBuilder( - RequestDelegateBuilder.BuildRequestDelegate(action), + RequestDelegateBuilder.Build(action), pattern, defaultOrder) { From b669d9d67248ba30b9c5a0c4173cf789f9dd7ac1 Mon Sep 17 00:00:00 2001 From: Stephen Halter Date: Thu, 25 Mar 2021 19:51:22 -0700 Subject: [PATCH 4/6] RequestDelegateBuilder -> RequestDelegateFactory --- .../src/PublicAPI.Unshipped.txt | 8 +- ...teBuilder.cs => RequestDelegateFactory.cs} | 16 ++-- ...ests.cs => RequestDelegateFactoryTests.cs} | 76 +++++++++---------- ...MapActionEndpointRouteBuilderExtensions.cs | 2 +- 4 files changed, 51 insertions(+), 51 deletions(-) rename src/Http/Http.Extensions/src/{RequestDelegateBuilder.cs => RequestDelegateFactory.cs} (98%) rename src/Http/Http.Extensions/test/{RequestDelegateBuilderTests.cs => RequestDelegateFactoryTests.cs} (94%) diff --git a/src/Http/Http.Extensions/src/PublicAPI.Unshipped.txt b/src/Http/Http.Extensions/src/PublicAPI.Unshipped.txt index d96bc0f11a95..bbab12bc1fe8 100644 --- a/src/Http/Http.Extensions/src/PublicAPI.Unshipped.txt +++ b/src/Http/Http.Extensions/src/PublicAPI.Unshipped.txt @@ -152,7 +152,7 @@ Microsoft.AspNetCore.Http.Headers.ResponseHeaders.Set(string! name, object? valu Microsoft.AspNetCore.Http.Headers.ResponseHeaders.SetCookie.get -> System.Collections.Generic.IList! Microsoft.AspNetCore.Http.Headers.ResponseHeaders.SetCookie.set -> void Microsoft.AspNetCore.Http.Headers.ResponseHeaders.SetList(string! name, System.Collections.Generic.IList? values) -> void -Microsoft.AspNetCore.Http.RequestDelegateBuilder +Microsoft.AspNetCore.Http.RequestDelegateFactory override Microsoft.AspNetCore.Http.Extensions.QueryBuilder.Equals(object? obj) -> bool override Microsoft.AspNetCore.Http.Extensions.QueryBuilder.ToString() -> string! static Microsoft.AspNetCore.Http.Extensions.HttpRequestMultipartExtensions.GetMultipartBoundary(this Microsoft.AspNetCore.Http.HttpRequest! request) -> string! @@ -169,9 +169,9 @@ static Microsoft.AspNetCore.Http.HeaderDictionaryTypeExtensions.AppendList(th static Microsoft.AspNetCore.Http.HeaderDictionaryTypeExtensions.GetTypedHeaders(this Microsoft.AspNetCore.Http.HttpRequest! request) -> Microsoft.AspNetCore.Http.Headers.RequestHeaders! static Microsoft.AspNetCore.Http.HeaderDictionaryTypeExtensions.GetTypedHeaders(this Microsoft.AspNetCore.Http.HttpResponse! response) -> Microsoft.AspNetCore.Http.Headers.ResponseHeaders! static Microsoft.AspNetCore.Http.HttpContextServerVariableExtensions.GetServerVariable(this Microsoft.AspNetCore.Http.HttpContext! context, string! variableName) -> string? -static Microsoft.AspNetCore.Http.RequestDelegateBuilder.Build(System.Delegate! action) -> Microsoft.AspNetCore.Http.RequestDelegate! -static Microsoft.AspNetCore.Http.RequestDelegateBuilder.Build(System.Reflection.MethodInfo! methodInfo) -> Microsoft.AspNetCore.Http.RequestDelegate! -static Microsoft.AspNetCore.Http.RequestDelegateBuilder.Build(System.Reflection.MethodInfo! methodInfo, System.Func! targetFactory) -> Microsoft.AspNetCore.Http.RequestDelegate! +static Microsoft.AspNetCore.Http.RequestDelegateFactory.Build(System.Delegate! action) -> Microsoft.AspNetCore.Http.RequestDelegate! +static Microsoft.AspNetCore.Http.RequestDelegateFactory.Build(System.Reflection.MethodInfo! methodInfo) -> Microsoft.AspNetCore.Http.RequestDelegate! +static Microsoft.AspNetCore.Http.RequestDelegateFactory.Build(System.Reflection.MethodInfo! methodInfo, System.Func! targetFactory) -> Microsoft.AspNetCore.Http.RequestDelegate! static Microsoft.AspNetCore.Http.ResponseExtensions.Clear(this Microsoft.AspNetCore.Http.HttpResponse! response) -> void static Microsoft.AspNetCore.Http.ResponseExtensions.Redirect(this Microsoft.AspNetCore.Http.HttpResponse! response, string! location, bool permanent, bool preserveMethod) -> void static Microsoft.AspNetCore.Http.SendFileResponseExtensions.SendFileAsync(this Microsoft.AspNetCore.Http.HttpResponse! response, Microsoft.Extensions.FileProviders.IFileInfo! file, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task! diff --git a/src/Http/Http.Extensions/src/RequestDelegateBuilder.cs b/src/Http/Http.Extensions/src/RequestDelegateFactory.cs similarity index 98% rename from src/Http/Http.Extensions/src/RequestDelegateBuilder.cs rename to src/Http/Http.Extensions/src/RequestDelegateFactory.cs index ff410e865674..a73d547872d9 100644 --- a/src/Http/Http.Extensions/src/RequestDelegateBuilder.cs +++ b/src/Http/Http.Extensions/src/RequestDelegateFactory.cs @@ -21,16 +21,16 @@ namespace Microsoft.AspNetCore.Http /// /// Builds implementations from request handlers. /// - public static class RequestDelegateBuilder + public static class RequestDelegateFactory { private static readonly MethodInfo ChangeTypeMethodInfo = GetMethodInfo>((value, type) => Convert.ChangeType(value, type, CultureInfo.InvariantCulture)); - private static readonly MethodInfo ExecuteTaskOfTMethodInfo = typeof(RequestDelegateBuilder).GetMethod(nameof(ExecuteTask), BindingFlags.NonPublic | BindingFlags.Static)!; - private static readonly MethodInfo ExecuteTaskOfStringMethodInfo = typeof(RequestDelegateBuilder).GetMethod(nameof(ExecuteTaskOfString), BindingFlags.NonPublic | BindingFlags.Static)!; - private static readonly MethodInfo ExecuteValueTaskOfTMethodInfo = typeof(RequestDelegateBuilder).GetMethod(nameof(ExecuteValueTaskOfT), BindingFlags.NonPublic | BindingFlags.Static)!; - private static readonly MethodInfo ExecuteValueTaskMethodInfo = typeof(RequestDelegateBuilder).GetMethod(nameof(ExecuteValueTask), BindingFlags.NonPublic | BindingFlags.Static)!; - private static readonly MethodInfo ExecuteValueTaskOfStringMethodInfo = typeof(RequestDelegateBuilder).GetMethod(nameof(ExecuteValueTaskOfString), BindingFlags.NonPublic | BindingFlags.Static)!; - private static readonly MethodInfo ExecuteTaskResultOfTMethodInfo = typeof(RequestDelegateBuilder).GetMethod(nameof(ExecuteTaskResult), BindingFlags.NonPublic | BindingFlags.Static)!; - private static readonly MethodInfo ExecuteValueResultTaskOfTMethodInfo = typeof(RequestDelegateBuilder).GetMethod(nameof(ExecuteValueTaskResult), BindingFlags.NonPublic | BindingFlags.Static)!; + private static readonly MethodInfo ExecuteTaskOfTMethodInfo = typeof(RequestDelegateFactory).GetMethod(nameof(ExecuteTask), BindingFlags.NonPublic | BindingFlags.Static)!; + private static readonly MethodInfo ExecuteTaskOfStringMethodInfo = typeof(RequestDelegateFactory).GetMethod(nameof(ExecuteTaskOfString), BindingFlags.NonPublic | BindingFlags.Static)!; + private static readonly MethodInfo ExecuteValueTaskOfTMethodInfo = typeof(RequestDelegateFactory).GetMethod(nameof(ExecuteValueTaskOfT), BindingFlags.NonPublic | BindingFlags.Static)!; + private static readonly MethodInfo ExecuteValueTaskMethodInfo = typeof(RequestDelegateFactory).GetMethod(nameof(ExecuteValueTask), BindingFlags.NonPublic | BindingFlags.Static)!; + private static readonly MethodInfo ExecuteValueTaskOfStringMethodInfo = typeof(RequestDelegateFactory).GetMethod(nameof(ExecuteValueTaskOfString), BindingFlags.NonPublic | BindingFlags.Static)!; + private static readonly MethodInfo ExecuteTaskResultOfTMethodInfo = typeof(RequestDelegateFactory).GetMethod(nameof(ExecuteTaskResult), BindingFlags.NonPublic | BindingFlags.Static)!; + private static readonly MethodInfo ExecuteValueResultTaskOfTMethodInfo = typeof(RequestDelegateFactory).GetMethod(nameof(ExecuteValueTaskResult), BindingFlags.NonPublic | BindingFlags.Static)!; private static readonly MethodInfo GetRequiredServiceMethodInfo = typeof(ServiceProviderServiceExtensions).GetMethod(nameof(ServiceProviderServiceExtensions.GetRequiredService), BindingFlags.Public | BindingFlags.Static, new Type[] { typeof(IServiceProvider) })!; private static readonly MethodInfo ResultWriteResponseAsync = typeof(IResult).GetMethod(nameof(IResult.ExecuteAsync), BindingFlags.Public | BindingFlags.Instance)!; private static readonly MethodInfo StringResultWriteResponseAsync = GetMethodInfo>((response, text) => HttpResponseWritingExtensions.WriteAsync(response, text, default)); diff --git a/src/Http/Http.Extensions/test/RequestDelegateBuilderTests.cs b/src/Http/Http.Extensions/test/RequestDelegateFactoryTests.cs similarity index 94% rename from src/Http/Http.Extensions/test/RequestDelegateBuilderTests.cs rename to src/Http/Http.Extensions/test/RequestDelegateFactoryTests.cs index a5eb6d981d2e..796d15dbeabe 100644 --- a/src/Http/Http.Extensions/test/RequestDelegateBuilderTests.cs +++ b/src/Http/Http.Extensions/test/RequestDelegateFactoryTests.cs @@ -23,7 +23,7 @@ namespace Microsoft.AspNetCore.Routing.Internal { - public class RequestDelegateBuilderTests + public class RequestDelegateFactoryTests { public static IEnumerable NoResult { @@ -86,7 +86,7 @@ public async Task RequestDelegateInvokesAction(Delegate @delegate) { var httpContext = new DefaultHttpContext(); - var requestDelegate = RequestDelegateBuilder.Build(@delegate); + var requestDelegate = RequestDelegateFactory.Build(@delegate); await requestDelegate(httpContext); @@ -101,12 +101,12 @@ private static void StaticTestActionBasicReflection(HttpContext httpContext) [Fact] public async Task StaticMethodInfoOverloadWorksWithBasicReflection() { - var methodInfo = typeof(RequestDelegateBuilderTests).GetMethod( + var methodInfo = typeof(RequestDelegateFactoryTests).GetMethod( nameof(StaticTestActionBasicReflection), BindingFlags.NonPublic | BindingFlags.Static, new[] { typeof(HttpContext) }); - var requestDelegate = RequestDelegateBuilder.Build(methodInfo!); + var requestDelegate = RequestDelegateFactory.Build(methodInfo!); var httpContext = new DefaultHttpContext(); @@ -151,7 +151,7 @@ object GetTarget() return new TestNonStaticActionClass(2); } - var requestDelegate = RequestDelegateBuilder.Build(methodInfo!, _ => GetTarget()); + var requestDelegate = RequestDelegateFactory.Build(methodInfo!, _ => GetTarget()); var httpContext = new DefaultHttpContext(); @@ -169,15 +169,15 @@ object GetTarget() [Fact] public void BuildRequestDelegateThrowsArgumentNullExceptions() { - var methodInfo = typeof(RequestDelegateBuilderTests).GetMethod( + var methodInfo = typeof(RequestDelegateFactoryTests).GetMethod( nameof(StaticTestActionBasicReflection), BindingFlags.NonPublic | BindingFlags.Static, new[] { typeof(HttpContext) }); - var exNullAction = Assert.Throws(() => RequestDelegateBuilder.Build(action: null!)); - var exNullMethodInfo1 = Assert.Throws(() => RequestDelegateBuilder.Build(methodInfo: null!)); - var exNullMethodInfo2 = Assert.Throws(() => RequestDelegateBuilder.Build(methodInfo: null!, _ => 0)); - var exNullTargetFactory = Assert.Throws(() => RequestDelegateBuilder.Build(methodInfo!, targetFactory: null!)); + var exNullAction = Assert.Throws(() => RequestDelegateFactory.Build(action: null!)); + var exNullMethodInfo1 = Assert.Throws(() => RequestDelegateFactory.Build(methodInfo: null!)); + var exNullMethodInfo2 = Assert.Throws(() => RequestDelegateFactory.Build(methodInfo: null!, _ => 0)); + var exNullTargetFactory = Assert.Throws(() => RequestDelegateFactory.Build(methodInfo!, targetFactory: null!)); Assert.Equal("action", exNullAction.ParamName); Assert.Equal("methodInfo", exNullMethodInfo1.ParamName); @@ -229,7 +229,7 @@ public async Task RequestDelegatePopulatesFromRouteParameterBasedOnParameterName var httpContext = new DefaultHttpContext(); httpContext.Request.RouteValues[paramName] = originalRouteParam.ToString(NumberFormatInfo.InvariantInfo); - var requestDelegate = RequestDelegateBuilder.Build(@delegate); + var requestDelegate = RequestDelegateFactory.Build(@delegate); await requestDelegate(httpContext); @@ -272,7 +272,7 @@ public async Task RequestDelegatePopulatesFromRouteOptionalParameter(Delegate @d { var httpContext = new DefaultHttpContext(); - var requestDelegate = RequestDelegateBuilder.Build(@delegate); + var requestDelegate = RequestDelegateFactory.Build(@delegate); await requestDelegate(httpContext); @@ -290,7 +290,7 @@ public async Task RequestDelegatePopulatesFromRouteOptionalParameterBasedOnParam httpContext.Request.RouteValues[paramName] = originalRouteParam.ToString(NumberFormatInfo.InvariantInfo); - var requestDelegate = RequestDelegateBuilder.Build(@delegate); + var requestDelegate = RequestDelegateFactory.Build(@delegate); await requestDelegate(httpContext); @@ -313,7 +313,7 @@ void TestAction([FromRoute(Name = specifiedName)] int foo) var httpContext = new DefaultHttpContext(); httpContext.Request.RouteValues[specifiedName] = originalRouteParam.ToString(NumberFormatInfo.InvariantInfo); - var requestDelegate = RequestDelegateBuilder.Build((Action)TestAction); + var requestDelegate = RequestDelegateFactory.Build((Action)TestAction); await requestDelegate(httpContext); @@ -336,7 +336,7 @@ void TestAction([FromRoute] int foo) var httpContext = new DefaultHttpContext(); httpContext.Request.RouteValues[unmatchedName] = unmatchedRouteParam.ToString(NumberFormatInfo.InvariantInfo); - var requestDelegate = RequestDelegateBuilder.Build((Action)TestAction); + var requestDelegate = RequestDelegateFactory.Build((Action)TestAction); await requestDelegate(httpContext); @@ -364,7 +364,7 @@ void TestAction([FromQuery] int value) var httpContext = new DefaultHttpContext(); httpContext.Request.Query = query; - var requestDelegate = RequestDelegateBuilder.Build((Action)TestAction); + var requestDelegate = RequestDelegateFactory.Build((Action)TestAction); await requestDelegate(httpContext); @@ -387,7 +387,7 @@ void TestAction([FromHeader(Name = customHeaderName)] int value) var httpContext = new DefaultHttpContext(); httpContext.Request.Headers[customHeaderName] = originalHeaderParam.ToString(NumberFormatInfo.InvariantInfo); - var requestDelegate = RequestDelegateBuilder.Build((Action)TestAction); + var requestDelegate = RequestDelegateFactory.Build((Action)TestAction); await requestDelegate(httpContext); @@ -415,7 +415,7 @@ void TestAction([FromBody] Todo todo) var requestBodyBytes = JsonSerializer.SerializeToUtf8Bytes(originalTodo); httpContext.Request.Body = new MemoryStream(requestBodyBytes); - var requestDelegate = RequestDelegateBuilder.Build((Action)TestAction); + var requestDelegate = RequestDelegateFactory.Build((Action)TestAction); await requestDelegate(httpContext); @@ -434,7 +434,7 @@ void TestAction([FromBody] Todo todo) httpContext.Request.Headers["Content-Type"] = "application/json"; httpContext.Request.Headers["Content-Length"] = "0"; - var requestDelegate = RequestDelegateBuilder.Build((Action)TestAction); + var requestDelegate = RequestDelegateFactory.Build((Action)TestAction); await Assert.ThrowsAsync(() => requestDelegate(httpContext)); } @@ -453,7 +453,7 @@ void TestAction([FromBody(AllowEmpty = true)] Todo todo) httpContext.Request.Headers["Content-Type"] = "application/json"; httpContext.Request.Headers["Content-Length"] = "0"; - var requestDelegate = RequestDelegateBuilder.Build((Action)TestAction); + var requestDelegate = RequestDelegateFactory.Build((Action)TestAction); await requestDelegate(httpContext); @@ -477,7 +477,7 @@ void TestAction([FromBody(AllowEmpty = true)] BodyStruct bodyStruct) httpContext.Request.Headers["Content-Type"] = "application/json"; httpContext.Request.Headers["Content-Length"] = "0"; - var requestDelegate = RequestDelegateBuilder.Build((Action)TestAction); + var requestDelegate = RequestDelegateFactory.Build((Action)TestAction); await requestDelegate(httpContext); @@ -507,7 +507,7 @@ void TestAction([FromBody] Todo todo) httpContext.Features.Set(new TestHttpRequestLifetimeFeature()); httpContext.RequestServices = serviceCollection.BuildServiceProvider(); - var requestDelegate = RequestDelegateBuilder.Build((Action)TestAction); + var requestDelegate = RequestDelegateFactory.Build((Action)TestAction); await requestDelegate(httpContext); @@ -543,7 +543,7 @@ void TestAction([FromBody] Todo todo) httpContext.Features.Set(new TestHttpRequestLifetimeFeature()); httpContext.RequestServices = serviceCollection.BuildServiceProvider(); - var requestDelegate = RequestDelegateBuilder.Build((Action)TestAction); + var requestDelegate = RequestDelegateFactory.Build((Action)TestAction); await requestDelegate(httpContext); @@ -578,7 +578,7 @@ void TestAction([FromForm] int value) var httpContext = new DefaultHttpContext(); httpContext.Request.Form = form; - var requestDelegate = RequestDelegateBuilder.Build((Action)TestAction); + var requestDelegate = RequestDelegateFactory.Build((Action)TestAction); await requestDelegate(httpContext); @@ -608,7 +608,7 @@ void TestAction([FromForm] int value) httpContext.Features.Set(new TestHttpRequestLifetimeFeature()); httpContext.RequestServices = serviceCollection.BuildServiceProvider(); - var requestDelegate = RequestDelegateBuilder.Build((Action)TestAction); + var requestDelegate = RequestDelegateFactory.Build((Action)TestAction); await requestDelegate(httpContext); @@ -644,7 +644,7 @@ void TestAction([FromForm] int value) httpContext.Features.Set(new TestHttpRequestLifetimeFeature()); httpContext.RequestServices = serviceCollection.BuildServiceProvider(); - var requestDelegate = RequestDelegateBuilder.Build((Action)TestAction); + var requestDelegate = RequestDelegateFactory.Build((Action)TestAction); await requestDelegate(httpContext); @@ -664,8 +664,8 @@ public void BuildRequestDelegateThrowsInvalidOperationExceptionGivenBothFromBody void TestAction([FromBody] int value1, [FromForm] int value2) { } void TestActionWithFlippedParams([FromForm] int value1, [FromBody] int value2) { } - Assert.Throws(() => RequestDelegateBuilder.Build((Action)TestAction)); - Assert.Throws(() => RequestDelegateBuilder.Build((Action)TestActionWithFlippedParams)); + Assert.Throws(() => RequestDelegateFactory.Build((Action)TestAction)); + Assert.Throws(() => RequestDelegateFactory.Build((Action)TestActionWithFlippedParams)); } [Fact] @@ -673,7 +673,7 @@ public void BuildRequestDelegateThrowsInvalidOperationExceptionGivenFromBodyOnMu { void TestAction([FromBody] int value1, [FromBody] int value2) { } - Assert.Throws(() => RequestDelegateBuilder.Build((Action)TestAction)); + Assert.Throws(() => RequestDelegateFactory.Build((Action)TestAction)); } [Fact] @@ -693,7 +693,7 @@ void TestAction([FromService] MyService myService) var httpContext = new DefaultHttpContext(); httpContext.RequestServices = serviceCollection.BuildServiceProvider(); - var requestDelegate = RequestDelegateBuilder.Build((Action)TestAction); + var requestDelegate = RequestDelegateFactory.Build((Action)TestAction); await requestDelegate(httpContext); @@ -712,7 +712,7 @@ void TestAction(HttpContext httpContext) var httpContext = new DefaultHttpContext(); - var requestDelegate = RequestDelegateBuilder.Build((Action)TestAction); + var requestDelegate = RequestDelegateFactory.Build((Action)TestAction); await requestDelegate(httpContext); @@ -732,7 +732,7 @@ void TestAction(IFormCollection httpContext) var httpContext = new DefaultHttpContext(); httpContext.Request.Headers["Content-Type"] = "application/x-www-form-urlencoded"; - var requestDelegate = RequestDelegateBuilder.Build((Action)TestAction); + var requestDelegate = RequestDelegateFactory.Build((Action)TestAction); await requestDelegate(httpContext); @@ -755,7 +755,7 @@ void TestAction(CancellationToken cancellationToken) RequestAborted = cts.Token }; - var requestDelegate = RequestDelegateBuilder.Build((Action)TestAction); + var requestDelegate = RequestDelegateFactory.Build((Action)TestAction); await requestDelegate(httpContext); @@ -799,7 +799,7 @@ public async Task RequestDelegateWritesComplexReturnValueAsJsonResponseBody(Dele var responseBodyStream = new MemoryStream(); httpContext.Response.Body = responseBodyStream; - var requestDelegate = RequestDelegateBuilder.Build(@delegate); + var requestDelegate = RequestDelegateFactory.Build(@delegate); await requestDelegate(httpContext); @@ -848,7 +848,7 @@ public async Task RequestDelegateUsesCustomIResult(Delegate @delegate) var responseBodyStream = new MemoryStream(); httpContext.Response.Body = responseBodyStream; - var requestDelegate = RequestDelegateBuilder.Build(@delegate); + var requestDelegate = RequestDelegateFactory.Build(@delegate); await requestDelegate(httpContext); @@ -891,7 +891,7 @@ public async Task RequestDelegateWritesStringReturnValueAsJsonResponseBody(Deleg var responseBodyStream = new MemoryStream(); httpContext.Response.Body = responseBodyStream; - var requestDelegate = RequestDelegateBuilder.Build(@delegate); + var requestDelegate = RequestDelegateFactory.Build(@delegate); await requestDelegate(httpContext); @@ -932,7 +932,7 @@ public async Task RequestDelegateWritesIntReturnValue(Delegate @delegate) var responseBodyStream = new MemoryStream(); httpContext.Response.Body = responseBodyStream; - var requestDelegate = RequestDelegateBuilder.Build(@delegate); + var requestDelegate = RequestDelegateFactory.Build(@delegate); await requestDelegate(httpContext); @@ -973,7 +973,7 @@ public async Task RequestDelegateWritesBoolReturnValue(Delegate @delegate) var responseBodyStream = new MemoryStream(); httpContext.Response.Body = responseBodyStream; - var requestDelegate = RequestDelegateBuilder.Build(@delegate); + var requestDelegate = RequestDelegateFactory.Build(@delegate); await requestDelegate(httpContext); diff --git a/src/Http/Routing/src/Builder/MapActionEndpointRouteBuilderExtensions.cs b/src/Http/Routing/src/Builder/MapActionEndpointRouteBuilderExtensions.cs index 0205ebdcd784..f05d1591e1b7 100644 --- a/src/Http/Routing/src/Builder/MapActionEndpointRouteBuilderExtensions.cs +++ b/src/Http/Routing/src/Builder/MapActionEndpointRouteBuilderExtensions.cs @@ -159,7 +159,7 @@ public static MapActionEndpointConventionBuilder Map( const int defaultOrder = 0; var builder = new RouteEndpointBuilder( - RequestDelegateBuilder.Build(action), + RequestDelegateFactory.Build(action), pattern, defaultOrder) { From 9e757bf20a592ce9784d9e8d926e24a1b6f0dfbd Mon Sep 17 00:00:00 2001 From: Stephen Halter Date: Thu, 25 Mar 2021 19:53:38 -0700 Subject: [PATCH 5/6] Build -> Create --- .../src/PublicAPI.Unshipped.txt | 6 +- .../src/RequestDelegateFactory.cs | 6 +- .../test/RequestDelegateFactoryTests.cs | 70 +++++++++---------- ...MapActionEndpointRouteBuilderExtensions.cs | 2 +- 4 files changed, 42 insertions(+), 42 deletions(-) diff --git a/src/Http/Http.Extensions/src/PublicAPI.Unshipped.txt b/src/Http/Http.Extensions/src/PublicAPI.Unshipped.txt index bbab12bc1fe8..338549737153 100644 --- a/src/Http/Http.Extensions/src/PublicAPI.Unshipped.txt +++ b/src/Http/Http.Extensions/src/PublicAPI.Unshipped.txt @@ -169,9 +169,9 @@ static Microsoft.AspNetCore.Http.HeaderDictionaryTypeExtensions.AppendList(th static Microsoft.AspNetCore.Http.HeaderDictionaryTypeExtensions.GetTypedHeaders(this Microsoft.AspNetCore.Http.HttpRequest! request) -> Microsoft.AspNetCore.Http.Headers.RequestHeaders! static Microsoft.AspNetCore.Http.HeaderDictionaryTypeExtensions.GetTypedHeaders(this Microsoft.AspNetCore.Http.HttpResponse! response) -> Microsoft.AspNetCore.Http.Headers.ResponseHeaders! static Microsoft.AspNetCore.Http.HttpContextServerVariableExtensions.GetServerVariable(this Microsoft.AspNetCore.Http.HttpContext! context, string! variableName) -> string? -static Microsoft.AspNetCore.Http.RequestDelegateFactory.Build(System.Delegate! action) -> Microsoft.AspNetCore.Http.RequestDelegate! -static Microsoft.AspNetCore.Http.RequestDelegateFactory.Build(System.Reflection.MethodInfo! methodInfo) -> Microsoft.AspNetCore.Http.RequestDelegate! -static Microsoft.AspNetCore.Http.RequestDelegateFactory.Build(System.Reflection.MethodInfo! methodInfo, System.Func! targetFactory) -> Microsoft.AspNetCore.Http.RequestDelegate! +static Microsoft.AspNetCore.Http.RequestDelegateFactory.Create(System.Delegate! action) -> Microsoft.AspNetCore.Http.RequestDelegate! +static Microsoft.AspNetCore.Http.RequestDelegateFactory.Create(System.Reflection.MethodInfo! methodInfo) -> Microsoft.AspNetCore.Http.RequestDelegate! +static Microsoft.AspNetCore.Http.RequestDelegateFactory.Create(System.Reflection.MethodInfo! methodInfo, System.Func! targetFactory) -> Microsoft.AspNetCore.Http.RequestDelegate! static Microsoft.AspNetCore.Http.ResponseExtensions.Clear(this Microsoft.AspNetCore.Http.HttpResponse! response) -> void static Microsoft.AspNetCore.Http.ResponseExtensions.Redirect(this Microsoft.AspNetCore.Http.HttpResponse! response, string! location, bool permanent, bool preserveMethod) -> void static Microsoft.AspNetCore.Http.SendFileResponseExtensions.SendFileAsync(this Microsoft.AspNetCore.Http.HttpResponse! response, Microsoft.Extensions.FileProviders.IFileInfo! file, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task! diff --git a/src/Http/Http.Extensions/src/RequestDelegateFactory.cs b/src/Http/Http.Extensions/src/RequestDelegateFactory.cs index a73d547872d9..f4c1449eeea8 100644 --- a/src/Http/Http.Extensions/src/RequestDelegateFactory.cs +++ b/src/Http/Http.Extensions/src/RequestDelegateFactory.cs @@ -51,7 +51,7 @@ public static class RequestDelegateFactory /// /// A request handler with any number of custom parameters that often produces a response with its return value. /// The - public static RequestDelegate Build(Delegate action) + public static RequestDelegate Create(Delegate action) { if (action is null) { @@ -77,7 +77,7 @@ public static RequestDelegate Build(Delegate action) /// /// A static request handler with any number of custom parameters that often produces a response with its return value. /// The - public static RequestDelegate Build(MethodInfo methodInfo) + public static RequestDelegate Create(MethodInfo methodInfo) { if (methodInfo is null) { @@ -98,7 +98,7 @@ public static RequestDelegate Build(MethodInfo methodInfo) /// A request handler with any number of custom parameters that often produces a response with its return value. /// Creates the for the non-static method. /// The - public static RequestDelegate Build(MethodInfo methodInfo, Func targetFactory) + public static RequestDelegate Create(MethodInfo methodInfo, Func targetFactory) { if (methodInfo is null) { diff --git a/src/Http/Http.Extensions/test/RequestDelegateFactoryTests.cs b/src/Http/Http.Extensions/test/RequestDelegateFactoryTests.cs index 796d15dbeabe..36d4004cfbe3 100644 --- a/src/Http/Http.Extensions/test/RequestDelegateFactoryTests.cs +++ b/src/Http/Http.Extensions/test/RequestDelegateFactoryTests.cs @@ -86,7 +86,7 @@ public async Task RequestDelegateInvokesAction(Delegate @delegate) { var httpContext = new DefaultHttpContext(); - var requestDelegate = RequestDelegateFactory.Build(@delegate); + var requestDelegate = RequestDelegateFactory.Create(@delegate); await requestDelegate(httpContext); @@ -106,7 +106,7 @@ public async Task StaticMethodInfoOverloadWorksWithBasicReflection() BindingFlags.NonPublic | BindingFlags.Static, new[] { typeof(HttpContext) }); - var requestDelegate = RequestDelegateFactory.Build(methodInfo!); + var requestDelegate = RequestDelegateFactory.Create(methodInfo!); var httpContext = new DefaultHttpContext(); @@ -151,7 +151,7 @@ object GetTarget() return new TestNonStaticActionClass(2); } - var requestDelegate = RequestDelegateFactory.Build(methodInfo!, _ => GetTarget()); + var requestDelegate = RequestDelegateFactory.Create(methodInfo!, _ => GetTarget()); var httpContext = new DefaultHttpContext(); @@ -174,10 +174,10 @@ public void BuildRequestDelegateThrowsArgumentNullExceptions() BindingFlags.NonPublic | BindingFlags.Static, new[] { typeof(HttpContext) }); - var exNullAction = Assert.Throws(() => RequestDelegateFactory.Build(action: null!)); - var exNullMethodInfo1 = Assert.Throws(() => RequestDelegateFactory.Build(methodInfo: null!)); - var exNullMethodInfo2 = Assert.Throws(() => RequestDelegateFactory.Build(methodInfo: null!, _ => 0)); - var exNullTargetFactory = Assert.Throws(() => RequestDelegateFactory.Build(methodInfo!, targetFactory: null!)); + var exNullAction = Assert.Throws(() => RequestDelegateFactory.Create(action: null!)); + var exNullMethodInfo1 = Assert.Throws(() => RequestDelegateFactory.Create(methodInfo: null!)); + var exNullMethodInfo2 = Assert.Throws(() => RequestDelegateFactory.Create(methodInfo: null!, _ => 0)); + var exNullTargetFactory = Assert.Throws(() => RequestDelegateFactory.Create(methodInfo!, targetFactory: null!)); Assert.Equal("action", exNullAction.ParamName); Assert.Equal("methodInfo", exNullMethodInfo1.ParamName); @@ -229,7 +229,7 @@ public async Task RequestDelegatePopulatesFromRouteParameterBasedOnParameterName var httpContext = new DefaultHttpContext(); httpContext.Request.RouteValues[paramName] = originalRouteParam.ToString(NumberFormatInfo.InvariantInfo); - var requestDelegate = RequestDelegateFactory.Build(@delegate); + var requestDelegate = RequestDelegateFactory.Create(@delegate); await requestDelegate(httpContext); @@ -272,7 +272,7 @@ public async Task RequestDelegatePopulatesFromRouteOptionalParameter(Delegate @d { var httpContext = new DefaultHttpContext(); - var requestDelegate = RequestDelegateFactory.Build(@delegate); + var requestDelegate = RequestDelegateFactory.Create(@delegate); await requestDelegate(httpContext); @@ -290,7 +290,7 @@ public async Task RequestDelegatePopulatesFromRouteOptionalParameterBasedOnParam httpContext.Request.RouteValues[paramName] = originalRouteParam.ToString(NumberFormatInfo.InvariantInfo); - var requestDelegate = RequestDelegateFactory.Build(@delegate); + var requestDelegate = RequestDelegateFactory.Create(@delegate); await requestDelegate(httpContext); @@ -313,7 +313,7 @@ void TestAction([FromRoute(Name = specifiedName)] int foo) var httpContext = new DefaultHttpContext(); httpContext.Request.RouteValues[specifiedName] = originalRouteParam.ToString(NumberFormatInfo.InvariantInfo); - var requestDelegate = RequestDelegateFactory.Build((Action)TestAction); + var requestDelegate = RequestDelegateFactory.Create((Action)TestAction); await requestDelegate(httpContext); @@ -336,7 +336,7 @@ void TestAction([FromRoute] int foo) var httpContext = new DefaultHttpContext(); httpContext.Request.RouteValues[unmatchedName] = unmatchedRouteParam.ToString(NumberFormatInfo.InvariantInfo); - var requestDelegate = RequestDelegateFactory.Build((Action)TestAction); + var requestDelegate = RequestDelegateFactory.Create((Action)TestAction); await requestDelegate(httpContext); @@ -364,7 +364,7 @@ void TestAction([FromQuery] int value) var httpContext = new DefaultHttpContext(); httpContext.Request.Query = query; - var requestDelegate = RequestDelegateFactory.Build((Action)TestAction); + var requestDelegate = RequestDelegateFactory.Create((Action)TestAction); await requestDelegate(httpContext); @@ -387,7 +387,7 @@ void TestAction([FromHeader(Name = customHeaderName)] int value) var httpContext = new DefaultHttpContext(); httpContext.Request.Headers[customHeaderName] = originalHeaderParam.ToString(NumberFormatInfo.InvariantInfo); - var requestDelegate = RequestDelegateFactory.Build((Action)TestAction); + var requestDelegate = RequestDelegateFactory.Create((Action)TestAction); await requestDelegate(httpContext); @@ -415,7 +415,7 @@ void TestAction([FromBody] Todo todo) var requestBodyBytes = JsonSerializer.SerializeToUtf8Bytes(originalTodo); httpContext.Request.Body = new MemoryStream(requestBodyBytes); - var requestDelegate = RequestDelegateFactory.Build((Action)TestAction); + var requestDelegate = RequestDelegateFactory.Create((Action)TestAction); await requestDelegate(httpContext); @@ -434,7 +434,7 @@ void TestAction([FromBody] Todo todo) httpContext.Request.Headers["Content-Type"] = "application/json"; httpContext.Request.Headers["Content-Length"] = "0"; - var requestDelegate = RequestDelegateFactory.Build((Action)TestAction); + var requestDelegate = RequestDelegateFactory.Create((Action)TestAction); await Assert.ThrowsAsync(() => requestDelegate(httpContext)); } @@ -453,7 +453,7 @@ void TestAction([FromBody(AllowEmpty = true)] Todo todo) httpContext.Request.Headers["Content-Type"] = "application/json"; httpContext.Request.Headers["Content-Length"] = "0"; - var requestDelegate = RequestDelegateFactory.Build((Action)TestAction); + var requestDelegate = RequestDelegateFactory.Create((Action)TestAction); await requestDelegate(httpContext); @@ -477,7 +477,7 @@ void TestAction([FromBody(AllowEmpty = true)] BodyStruct bodyStruct) httpContext.Request.Headers["Content-Type"] = "application/json"; httpContext.Request.Headers["Content-Length"] = "0"; - var requestDelegate = RequestDelegateFactory.Build((Action)TestAction); + var requestDelegate = RequestDelegateFactory.Create((Action)TestAction); await requestDelegate(httpContext); @@ -507,7 +507,7 @@ void TestAction([FromBody] Todo todo) httpContext.Features.Set(new TestHttpRequestLifetimeFeature()); httpContext.RequestServices = serviceCollection.BuildServiceProvider(); - var requestDelegate = RequestDelegateFactory.Build((Action)TestAction); + var requestDelegate = RequestDelegateFactory.Create((Action)TestAction); await requestDelegate(httpContext); @@ -543,7 +543,7 @@ void TestAction([FromBody] Todo todo) httpContext.Features.Set(new TestHttpRequestLifetimeFeature()); httpContext.RequestServices = serviceCollection.BuildServiceProvider(); - var requestDelegate = RequestDelegateFactory.Build((Action)TestAction); + var requestDelegate = RequestDelegateFactory.Create((Action)TestAction); await requestDelegate(httpContext); @@ -578,7 +578,7 @@ void TestAction([FromForm] int value) var httpContext = new DefaultHttpContext(); httpContext.Request.Form = form; - var requestDelegate = RequestDelegateFactory.Build((Action)TestAction); + var requestDelegate = RequestDelegateFactory.Create((Action)TestAction); await requestDelegate(httpContext); @@ -608,7 +608,7 @@ void TestAction([FromForm] int value) httpContext.Features.Set(new TestHttpRequestLifetimeFeature()); httpContext.RequestServices = serviceCollection.BuildServiceProvider(); - var requestDelegate = RequestDelegateFactory.Build((Action)TestAction); + var requestDelegate = RequestDelegateFactory.Create((Action)TestAction); await requestDelegate(httpContext); @@ -644,7 +644,7 @@ void TestAction([FromForm] int value) httpContext.Features.Set(new TestHttpRequestLifetimeFeature()); httpContext.RequestServices = serviceCollection.BuildServiceProvider(); - var requestDelegate = RequestDelegateFactory.Build((Action)TestAction); + var requestDelegate = RequestDelegateFactory.Create((Action)TestAction); await requestDelegate(httpContext); @@ -664,8 +664,8 @@ public void BuildRequestDelegateThrowsInvalidOperationExceptionGivenBothFromBody void TestAction([FromBody] int value1, [FromForm] int value2) { } void TestActionWithFlippedParams([FromForm] int value1, [FromBody] int value2) { } - Assert.Throws(() => RequestDelegateFactory.Build((Action)TestAction)); - Assert.Throws(() => RequestDelegateFactory.Build((Action)TestActionWithFlippedParams)); + Assert.Throws(() => RequestDelegateFactory.Create((Action)TestAction)); + Assert.Throws(() => RequestDelegateFactory.Create((Action)TestActionWithFlippedParams)); } [Fact] @@ -673,7 +673,7 @@ public void BuildRequestDelegateThrowsInvalidOperationExceptionGivenFromBodyOnMu { void TestAction([FromBody] int value1, [FromBody] int value2) { } - Assert.Throws(() => RequestDelegateFactory.Build((Action)TestAction)); + Assert.Throws(() => RequestDelegateFactory.Create((Action)TestAction)); } [Fact] @@ -693,7 +693,7 @@ void TestAction([FromService] MyService myService) var httpContext = new DefaultHttpContext(); httpContext.RequestServices = serviceCollection.BuildServiceProvider(); - var requestDelegate = RequestDelegateFactory.Build((Action)TestAction); + var requestDelegate = RequestDelegateFactory.Create((Action)TestAction); await requestDelegate(httpContext); @@ -712,7 +712,7 @@ void TestAction(HttpContext httpContext) var httpContext = new DefaultHttpContext(); - var requestDelegate = RequestDelegateFactory.Build((Action)TestAction); + var requestDelegate = RequestDelegateFactory.Create((Action)TestAction); await requestDelegate(httpContext); @@ -732,7 +732,7 @@ void TestAction(IFormCollection httpContext) var httpContext = new DefaultHttpContext(); httpContext.Request.Headers["Content-Type"] = "application/x-www-form-urlencoded"; - var requestDelegate = RequestDelegateFactory.Build((Action)TestAction); + var requestDelegate = RequestDelegateFactory.Create((Action)TestAction); await requestDelegate(httpContext); @@ -755,7 +755,7 @@ void TestAction(CancellationToken cancellationToken) RequestAborted = cts.Token }; - var requestDelegate = RequestDelegateFactory.Build((Action)TestAction); + var requestDelegate = RequestDelegateFactory.Create((Action)TestAction); await requestDelegate(httpContext); @@ -799,7 +799,7 @@ public async Task RequestDelegateWritesComplexReturnValueAsJsonResponseBody(Dele var responseBodyStream = new MemoryStream(); httpContext.Response.Body = responseBodyStream; - var requestDelegate = RequestDelegateFactory.Build(@delegate); + var requestDelegate = RequestDelegateFactory.Create(@delegate); await requestDelegate(httpContext); @@ -848,7 +848,7 @@ public async Task RequestDelegateUsesCustomIResult(Delegate @delegate) var responseBodyStream = new MemoryStream(); httpContext.Response.Body = responseBodyStream; - var requestDelegate = RequestDelegateFactory.Build(@delegate); + var requestDelegate = RequestDelegateFactory.Create(@delegate); await requestDelegate(httpContext); @@ -891,7 +891,7 @@ public async Task RequestDelegateWritesStringReturnValueAsJsonResponseBody(Deleg var responseBodyStream = new MemoryStream(); httpContext.Response.Body = responseBodyStream; - var requestDelegate = RequestDelegateFactory.Build(@delegate); + var requestDelegate = RequestDelegateFactory.Create(@delegate); await requestDelegate(httpContext); @@ -932,7 +932,7 @@ public async Task RequestDelegateWritesIntReturnValue(Delegate @delegate) var responseBodyStream = new MemoryStream(); httpContext.Response.Body = responseBodyStream; - var requestDelegate = RequestDelegateFactory.Build(@delegate); + var requestDelegate = RequestDelegateFactory.Create(@delegate); await requestDelegate(httpContext); @@ -973,7 +973,7 @@ public async Task RequestDelegateWritesBoolReturnValue(Delegate @delegate) var responseBodyStream = new MemoryStream(); httpContext.Response.Body = responseBodyStream; - var requestDelegate = RequestDelegateFactory.Build(@delegate); + var requestDelegate = RequestDelegateFactory.Create(@delegate); await requestDelegate(httpContext); diff --git a/src/Http/Routing/src/Builder/MapActionEndpointRouteBuilderExtensions.cs b/src/Http/Routing/src/Builder/MapActionEndpointRouteBuilderExtensions.cs index f05d1591e1b7..e071bae5ac81 100644 --- a/src/Http/Routing/src/Builder/MapActionEndpointRouteBuilderExtensions.cs +++ b/src/Http/Routing/src/Builder/MapActionEndpointRouteBuilderExtensions.cs @@ -159,7 +159,7 @@ public static MapActionEndpointConventionBuilder Map( const int defaultOrder = 0; var builder = new RouteEndpointBuilder( - RequestDelegateFactory.Build(action), + RequestDelegateFactory.Create(action), pattern, defaultOrder) { From 65a873ffdff57bc2e489614f908db3c56f6a4edf Mon Sep 17 00:00:00 2001 From: Stephen Halter Date: Mon, 29 Mar 2021 12:04:04 -0700 Subject: [PATCH 6/6] Address final review feedback. --- .../src/RequestDelegateFactory.cs | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Http/Http.Extensions/src/RequestDelegateFactory.cs b/src/Http/Http.Extensions/src/RequestDelegateFactory.cs index f4c1449eeea8..948c77fd094c 100644 --- a/src/Http/Http.Extensions/src/RequestDelegateFactory.cs +++ b/src/Http/Http.Extensions/src/RequestDelegateFactory.cs @@ -19,7 +19,7 @@ namespace Microsoft.AspNetCore.Http { /// - /// Builds implementations from request handlers. + /// Creates implementations from request handlers. /// public static class RequestDelegateFactory { @@ -47,10 +47,10 @@ public static class RequestDelegateFactory private static readonly MemberExpression RequestAbortedExpr = Expression.Property(HttpContextParameter, nameof(HttpContext.RequestAborted)); /// - /// Builds a implementation for . + /// Creates a implementation for . /// /// A request handler with any number of custom parameters that often produces a response with its return value. - /// The + /// The . public static RequestDelegate Create(Delegate action) { if (action is null) @@ -60,11 +60,11 @@ public static RequestDelegate Create(Delegate action) var targetExpression = action.Target switch { - { } => Expression.Convert(TargetArg, action.Target.GetType()), + object => Expression.Convert(TargetArg, action.Target.GetType()), null => null, }; - var untargetedRequestDelegate = BuildRequestDelegate(action.Method, targetExpression); + var untargetedRequestDelegate = CreateRequestDelegate(action.Method, targetExpression); return httpContext => { @@ -73,10 +73,10 @@ public static RequestDelegate Create(Delegate action) } /// - /// Builds a implementation for . + /// Creates a implementation for . /// /// A static request handler with any number of custom parameters that often produces a response with its return value. - /// The + /// The . public static RequestDelegate Create(MethodInfo methodInfo) { if (methodInfo is null) @@ -84,7 +84,7 @@ public static RequestDelegate Create(MethodInfo methodInfo) throw new ArgumentNullException(nameof(methodInfo)); } - var untargetedRequestDelegate = BuildRequestDelegate(methodInfo, targetExpression: null); + var untargetedRequestDelegate = CreateRequestDelegate(methodInfo, targetExpression: null); return httpContext => { @@ -93,11 +93,11 @@ public static RequestDelegate Create(MethodInfo methodInfo) } /// - /// Builds a implementation for . + /// Creates a implementation for . /// /// A request handler with any number of custom parameters that often produces a response with its return value. /// Creates the for the non-static method. - /// The + /// The . public static RequestDelegate Create(MethodInfo methodInfo, Func targetFactory) { if (methodInfo is null) @@ -116,7 +116,7 @@ public static RequestDelegate Create(MethodInfo methodInfo, Func { @@ -124,7 +124,7 @@ public static RequestDelegate Create(MethodInfo methodInfo, Func BuildRequestDelegate(MethodInfo methodInfo, Expression? targetExpression) + private static Func CreateRequestDelegate(MethodInfo methodInfo, Expression? targetExpression) { // Non void return type