Skip to content

Commit ba37a08

Browse files
authored
Add support for RouteHandlerInvocationContext<> overloads (#41406)
* Add support for RouteHandlerInvocationContext<> overloads * Address feedback from review * Address feedback from API review * Rename RouteHandlerInvocationContextBase per API review * Address more feedback from review * Address more feedack
1 parent a6d71fb commit ba37a08

11 files changed

+1825
-45
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Microsoft.AspNetCore.Http;
5+
6+
/// <summary>
7+
/// Provides a default implementation for wrapping the <see cref="HttpContext"/> and parameters
8+
/// provided to a route handler.
9+
/// </summary>
10+
public sealed class DefaultRouteHandlerInvocationContext : RouteHandlerInvocationContext
11+
{
12+
/// <summary>
13+
/// Creates a new instance of the <see cref="DefaultRouteHandlerInvocationContext"/> for a given request.
14+
/// </summary>
15+
/// <param name="httpContext">The <see cref="HttpContext"/> associated with the current request.</param>
16+
/// <param name="arguments">A list of parameters provided in the current request.</param>
17+
public DefaultRouteHandlerInvocationContext(HttpContext httpContext, params object[] arguments)
18+
{
19+
HttpContext = httpContext;
20+
Arguments = arguments;
21+
}
22+
23+
/// <inheritdoc />
24+
public override HttpContext HttpContext { get; }
25+
26+
/// <inheritdoc />
27+
public override IList<object?> Arguments { get; }
28+
29+
/// <inheritdoc />
30+
public override T GetArgument<T>(int index)
31+
{
32+
return (T)Arguments[index]!;
33+
}
34+
}

src/Http/Http.Abstractions/src/Microsoft.AspNetCore.Http.Abstractions.csproj

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,26 @@ Microsoft.AspNetCore.Http.HttpResponse</Description>
3030

3131
<ItemGroup>
3232
<InternalsVisibleTo Include="Microsoft.AspNetCore.Http.Abstractions.Microbenchmarks" />
33+
<InternalsVisibleTo Include="Microsoft.AspNetCore.Http.Extensions" />
34+
</ItemGroup>
35+
36+
<ItemGroup>
37+
<None Update="RouteHandlerInvocationContextOfT.Generated.tt">
38+
<Generator>TextTemplatingFileGenerator</Generator>
39+
<LastGenOutput>RouteHandlerInvocationContextOfT.Generated.cs</LastGenOutput>
40+
</None>
41+
</ItemGroup>
42+
43+
<ItemGroup>
44+
<Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" />
45+
</ItemGroup>
46+
47+
<ItemGroup>
48+
<Compile Update="RouteHandlerInvocationContextOfT.Generated.cs">
49+
<DesignTime>True</DesignTime>
50+
<AutoGen>True</AutoGen>
51+
<DependentUpon>RouteHandlerInvocationContextOfT.Generated.tt</DependentUpon>
52+
</Compile>
3353
</ItemGroup>
3454

3555
</Project>

src/Http/Http.Abstractions/src/PublicAPI.Unshipped.txt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
*REMOVED*Microsoft.AspNetCore.Http.EndpointMetadataCollection.Enumerator.Current.get -> object?
44
Microsoft.AspNetCore.Builder.EndpointBuilder.ServiceProvider.get -> System.IServiceProvider?
55
Microsoft.AspNetCore.Builder.EndpointBuilder.ServiceProvider.set -> void
6+
Microsoft.AspNetCore.Http.DefaultRouteHandlerInvocationContext
7+
Microsoft.AspNetCore.Http.DefaultRouteHandlerInvocationContext.DefaultRouteHandlerInvocationContext(Microsoft.AspNetCore.Http.HttpContext! httpContext, params object![]! arguments) -> void
68
Microsoft.AspNetCore.Http.EndpointMetadataCollection.Enumerator.Current.get -> object!
79
Microsoft.AspNetCore.Http.EndpointMetadataCollection.GetRequiredMetadata<T>() -> T!
810
Microsoft.AspNetCore.Http.IRouteHandlerFilter.InvokeAsync(Microsoft.AspNetCore.Http.RouteHandlerInvocationContext! context, Microsoft.AspNetCore.Http.RouteHandlerFilterDelegate! next) -> System.Threading.Tasks.ValueTask<object?>
@@ -14,9 +16,7 @@ Microsoft.AspNetCore.Http.RouteHandlerContext.MethodInfo.get -> System.Reflectio
1416
Microsoft.AspNetCore.Http.RouteHandlerContext.RouteHandlerContext(System.Reflection.MethodInfo! methodInfo, Microsoft.AspNetCore.Http.EndpointMetadataCollection! endpointMetadata) -> void
1517
Microsoft.AspNetCore.Http.RouteHandlerFilterDelegate
1618
Microsoft.AspNetCore.Http.RouteHandlerInvocationContext
17-
Microsoft.AspNetCore.Http.RouteHandlerInvocationContext.HttpContext.get -> Microsoft.AspNetCore.Http.HttpContext!
18-
Microsoft.AspNetCore.Http.RouteHandlerInvocationContext.Parameters.get -> System.Collections.Generic.IList<object?>!
19-
Microsoft.AspNetCore.Http.RouteHandlerInvocationContext.RouteHandlerInvocationContext(Microsoft.AspNetCore.Http.HttpContext! httpContext, params object![]! parameters) -> void
19+
Microsoft.AspNetCore.Http.RouteHandlerInvocationContext.RouteHandlerInvocationContext() -> void
2020
Microsoft.AspNetCore.Routing.RouteValueDictionary.RouteValueDictionary(Microsoft.AspNetCore.Routing.RouteValueDictionary? dictionary) -> void
2121
Microsoft.AspNetCore.Routing.RouteValueDictionary.RouteValueDictionary(System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string!, object?>>? values) -> void
2222
Microsoft.AspNetCore.Routing.RouteValueDictionary.RouteValueDictionary(System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string!, string?>>? values) -> void
@@ -27,3 +27,9 @@ Microsoft.AspNetCore.Http.Metadata.IEndpointDescriptionMetadata
2727
Microsoft.AspNetCore.Http.Metadata.IEndpointDescriptionMetadata.Description.get -> string!
2828
Microsoft.AspNetCore.Http.Metadata.IEndpointSummaryMetadata
2929
Microsoft.AspNetCore.Http.Metadata.IEndpointSummaryMetadata.Summary.get -> string!
30+
abstract Microsoft.AspNetCore.Http.RouteHandlerInvocationContext.Arguments.get -> System.Collections.Generic.IList<object?>!
31+
abstract Microsoft.AspNetCore.Http.RouteHandlerInvocationContext.GetArgument<T>(int index) -> T
32+
abstract Microsoft.AspNetCore.Http.RouteHandlerInvocationContext.HttpContext.get -> Microsoft.AspNetCore.Http.HttpContext!
33+
override Microsoft.AspNetCore.Http.DefaultRouteHandlerInvocationContext.Arguments.get -> System.Collections.Generic.IList<object?>!
34+
override Microsoft.AspNetCore.Http.DefaultRouteHandlerInvocationContext.GetArgument<T>(int index) -> T
35+
override Microsoft.AspNetCore.Http.DefaultRouteHandlerInvocationContext.HttpContext.get -> Microsoft.AspNetCore.Http.HttpContext!

src/Http/Http.Abstractions/src/RouteHandlerInvocationContext.cs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,29 @@
44
namespace Microsoft.AspNetCore.Http;
55

66
/// <summary>
7-
/// Provides an abstraction for wrapping the <see cref="HttpContext"/> and parameters
7+
/// Provides an abstraction for wrapping the <see cref="HttpContext"/> and arguments
88
/// provided to a route handler.
99
/// </summary>
10-
public sealed class RouteHandlerInvocationContext
10+
public abstract class RouteHandlerInvocationContext
1111
{
12-
/// <summary>
13-
/// Creates a new instance of the <see cref="RouteHandlerInvocationContext"/> for a given request.
14-
/// </summary>
15-
/// <param name="httpContext">The <see cref="HttpContext"/> associated with the current request.</param>
16-
/// <param name="parameters">A list of parameters provided in the current request.</param>
17-
public RouteHandlerInvocationContext(HttpContext httpContext, params object[] parameters)
18-
{
19-
HttpContext = httpContext;
20-
Parameters = parameters;
21-
}
22-
2312
/// <summary>
2413
/// The <see cref="HttpContext"/> associated with the current request being processed by the filter.
2514
/// </summary>
26-
public HttpContext HttpContext { get; }
15+
public abstract HttpContext HttpContext { get; }
2716

2817
/// <summary>
29-
/// A list of parameters provided in the current request to the filter.
18+
/// A list of arguments provided in the current request to the filter.
3019
/// <remarks>
31-
/// This list is not read-only to permit modifying of existing parameters by filters.
20+
/// This list is not read-only to permit modifying of existing arguments by filters.
3221
/// </remarks>
3322
/// </summary>
34-
public IList<object?> Parameters { get; }
23+
public abstract IList<object?> Arguments { get; }
24+
25+
/// <summary>
26+
/// Retrieve the argument given its position in the argument list.
27+
/// </summary>
28+
/// <typeparam name="T">The <see cref="Type"/> of the resolved argument.</typeparam>
29+
/// <param name="index">An integer representing the position of the argument in the argument list.</param>
30+
/// <returns>The argument at a given <paramref name="index"/>.</returns>
31+
public abstract T GetArgument<T>(int index);
3532
}

0 commit comments

Comments
 (0)