@@ -79,13 +79,13 @@ public static partial class RequestDelegateFactory
7979 private static readonly BinaryExpression TempSourceStringNullExpr = Expression . Equal ( TempSourceStringExpr , Expression . Constant ( null ) ) ;
8080 private static readonly UnaryExpression TempSourceStringIsNotNullOrEmptyExpr = Expression . Not ( Expression . Call ( StringIsNullOrEmptyMethod , TempSourceStringExpr ) ) ;
8181
82- private static readonly ConstructorInfo RouteHandlerFilterContextConstructor = typeof ( RouteHandlerFilterContext ) . GetConstructor ( new [ ] { typeof ( HttpContext ) , typeof ( object [ ] ) } ) ! ;
83- private static readonly ParameterExpression FilterContextExpr = Expression . Parameter ( typeof ( RouteHandlerFilterContext ) , "context" ) ;
84- private static readonly MemberExpression FilterContextParametersExpr = Expression . Property ( FilterContextExpr , typeof ( RouteHandlerFilterContext ) . GetProperty ( nameof ( RouteHandlerFilterContext . Parameters ) ) ! ) ;
85- private static readonly MemberExpression FilterContextHttpContextExpr = Expression . Property ( FilterContextExpr , typeof ( RouteHandlerFilterContext ) . GetProperty ( nameof ( RouteHandlerFilterContext . HttpContext ) ) ! ) ;
82+ private static readonly ConstructorInfo RouteHandlerInvocationContextConstructor = typeof ( RouteHandlerInvocationContext ) . GetConstructor ( new [ ] { typeof ( HttpContext ) , typeof ( object [ ] ) } ) ! ;
83+ private static readonly ParameterExpression FilterContextExpr = Expression . Parameter ( typeof ( RouteHandlerInvocationContext ) , "context" ) ;
84+ private static readonly MemberExpression FilterContextParametersExpr = Expression . Property ( FilterContextExpr , typeof ( RouteHandlerInvocationContext ) . GetProperty ( nameof ( RouteHandlerInvocationContext . Parameters ) ) ! ) ;
85+ private static readonly MemberExpression FilterContextHttpContextExpr = Expression . Property ( FilterContextExpr , typeof ( RouteHandlerInvocationContext ) . GetProperty ( nameof ( RouteHandlerInvocationContext . HttpContext ) ) ! ) ;
8686 private static readonly MemberExpression FilterContextHttpContextResponseExpr = Expression . Property ( FilterContextHttpContextExpr , typeof ( HttpContext ) . GetProperty ( nameof ( HttpContext . Response ) ) ! ) ;
8787 private static readonly MemberExpression FilterContextHttpContextStatusCodeExpr = Expression . Property ( FilterContextHttpContextResponseExpr , typeof ( HttpResponse ) . GetProperty ( nameof ( HttpResponse . StatusCode ) ) ! ) ;
88- private static readonly ParameterExpression InvokedFilterContextExpr = Expression . Parameter ( typeof ( RouteHandlerFilterContext ) , "filterContext" ) ;
88+ private static readonly ParameterExpression InvokedFilterContextExpr = Expression . Parameter ( typeof ( RouteHandlerInvocationContext ) , "filterContext" ) ;
8989
9090 private static readonly string [ ] DefaultAcceptsContentType = new [ ] { "application/json" } ;
9191 private static readonly string [ ] FormFileContentType = new [ ] { "multipart/form-data" } ;
@@ -166,7 +166,7 @@ private static FactoryContext CreateFactoryContext(RequestDelegateFactoryOptions
166166 RouteParameters = options ? . RouteParameterNames ? . ToList ( ) ,
167167 ThrowOnBadRequest = options ? . ThrowOnBadRequest ?? false ,
168168 DisableInferredFromBody = options ? . DisableInferBodyFromParameters ?? false ,
169- Filters = options ? . RouteHandlerFilters ? . ToList ( )
169+ Filters = options ? . RouteHandlerFilterFactories ? . ToList ( )
170170 } ;
171171
172172 private static Func < object ? , HttpContext , Task > CreateTargetableRequestDelegate ( MethodInfo methodInfo , Expression ? targetExpression , FactoryContext factoryContext )
@@ -196,15 +196,15 @@ private static FactoryContext CreateFactoryContext(RequestDelegateFactoryOptions
196196 if ( factoryContext . Filters is { Count : > 0 } )
197197 {
198198 var filterPipeline = CreateFilterPipeline ( methodInfo , targetExpression , factoryContext ) ;
199- Expression < Func < RouteHandlerFilterContext , ValueTask < object ? > > > invokePipeline = ( context ) => filterPipeline ( context ) ;
199+ Expression < Func < RouteHandlerInvocationContext , ValueTask < object ? > > > invokePipeline = ( context ) => filterPipeline ( context ) ;
200200 returnType = typeof ( ValueTask < object ? > ) ;
201- // var filterContext = new RouteHandlerFilterContext (httpContext, new[] { (object)name_local, (object)int_local });
201+ // var filterContext = new RouteHandlerInvocationContext (httpContext, new[] { (object)name_local, (object)int_local });
202202 // invokePipeline.Invoke(filterContext);
203203 factoryContext . MethodCall = Expression . Block (
204204 new [ ] { InvokedFilterContextExpr } ,
205205 Expression . Assign (
206206 InvokedFilterContextExpr ,
207- Expression . New ( RouteHandlerFilterContextConstructor ,
207+ Expression . New ( RouteHandlerInvocationContextConstructor ,
208208 new Expression [ ] { HttpContextExpr , Expression . NewArrayInit ( typeof ( object ) , factoryContext . BoxedArgs ) } ) ) ,
209209 Expression . Invoke ( invokePipeline , InvokedFilterContextExpr )
210210 ) ;
@@ -222,13 +222,13 @@ private static FactoryContext CreateFactoryContext(RequestDelegateFactoryOptions
222222 return HandleRequestBodyAndCompileRequestDelegate ( responseWritingMethodCall , factoryContext ) ;
223223 }
224224
225- private static Func < RouteHandlerFilterContext , ValueTask < object ? > > CreateFilterPipeline ( MethodInfo methodInfo , Expression ? target , FactoryContext factoryContext )
225+ private static RouteHandlerFilterDelegate CreateFilterPipeline ( MethodInfo methodInfo , Expression ? target , FactoryContext factoryContext )
226226 {
227227 Debug . Assert ( factoryContext . Filters is not null ) ;
228228 // httpContext.Response.StatusCode >= 400
229229 // ? Task.CompletedTask
230230 // : handler((string)context.Parameters[0], (int)context.Parameters[1])
231- var filteredInvocation = Expression . Lambda < Func < RouteHandlerFilterContext , ValueTask < object ? > > > (
231+ var filteredInvocation = Expression . Lambda < RouteHandlerFilterDelegate > (
232232 Expression . Condition (
233233 Expression . GreaterThanOrEqual ( FilterContextHttpContextStatusCodeExpr , Expression . Constant ( 400 ) ) ,
234234 CompletedValueTaskExpr ,
@@ -240,12 +240,16 @@ target is null
240240 : Expression . Call ( target , methodInfo , factoryContext . ContextArgAccess ) )
241241 ) ) ,
242242 FilterContextExpr ) . Compile ( ) ;
243+ var routeHandlerContext = new RouteHandlerContext (
244+ methodInfo ,
245+ new EndpointMetadataCollection ( factoryContext . Metadata ) ) ;
243246
244247 for ( var i = factoryContext . Filters . Count - 1 ; i >= 0 ; i -- )
245248 {
246- var currentFilter = factoryContext . Filters ! [ i ] ;
249+ var currentFilterFactory = factoryContext . Filters [ i ] ;
247250 var nextFilter = filteredInvocation ;
248- filteredInvocation = ( RouteHandlerFilterContext context ) => currentFilter . InvokeAsync ( context , nextFilter ) ;
251+ var currentFilter = currentFilterFactory ( routeHandlerContext , nextFilter ) ;
252+ filteredInvocation = ( RouteHandlerInvocationContext context ) => currentFilter ( context ) ;
249253
250254 }
251255 return filteredInvocation ;
@@ -264,7 +268,7 @@ private static Expression[] CreateArguments(ParameterInfo[]? parameters, Factory
264268 {
265269 args [ i ] = CreateArgument ( parameters [ i ] , factoryContext ) ;
266270 // Register expressions containing the boxed and unboxed variants
267- // of the route handler's arguments for use in RouteHandlerFilterContext
271+ // of the route handler's arguments for use in RouteHandlerInvocationContext
268272 // construction and route handler invocation.
269273 // (string)context.Parameters[0];
270274 factoryContext . ContextArgAccess . Add (
@@ -1693,7 +1697,7 @@ private class FactoryContext
16931697 public List < Expression > ContextArgAccess { get ; } = new ( ) ;
16941698 public Expression ? MethodCall { get ; set ; }
16951699 public List < Expression > BoxedArgs { get ; } = new ( ) ;
1696- public List < IRouteHandlerFilter > ? Filters { get ; init ; }
1700+ public List < Func < RouteHandlerContext , RouteHandlerFilterDelegate , RouteHandlerFilterDelegate > > ? Filters { get ; init ; }
16971701 }
16981702
16991703 private static class RequestDelegateFactoryConstants
0 commit comments