1010using System . Security . Claims ;
1111using System . Text ;
1212using System . Text . Json ;
13+ using Microsoft . AspNetCore . Http . Abstractions ;
1314using Microsoft . AspNetCore . Http . Features ;
1415using Microsoft . AspNetCore . Http . Metadata ;
1516using Microsoft . Extensions . DependencyInjection ;
@@ -79,7 +80,7 @@ public static partial class RequestDelegateFactory
7980 private static readonly BinaryExpression TempSourceStringNullExpr = Expression . Equal ( TempSourceStringExpr , Expression . Constant ( null ) ) ;
8081 private static readonly UnaryExpression TempSourceStringIsNotNullOrEmptyExpr = Expression . Not ( Expression . Call ( StringIsNullOrEmptyMethod , TempSourceStringExpr ) ) ;
8182
82- private static readonly ConstructorInfo RouteHandlerFilterContextConstructor = typeof ( RouteHandlerFilterContext ) . GetConstructor ( new [ ] { typeof ( HttpContext ) , typeof ( object [ ] ) } ) ! ;
83+ private static readonly ConstructorInfo RouteHandlerFilterContextConstructor = typeof ( RouteHandlerFilterContext ) . GetConstructor ( new [ ] { typeof ( HttpContext ) , typeof ( IServiceProvider ) , typeof ( object [ ] ) } ) ! ;
8384 private static readonly ParameterExpression FilterContextExpr = Expression . Parameter ( typeof ( RouteHandlerFilterContext ) , "context" ) ;
8485 private static readonly MemberExpression FilterContextParametersExpr = Expression . Property ( FilterContextExpr , typeof ( RouteHandlerFilterContext ) . GetProperty ( nameof ( RouteHandlerFilterContext . Parameters ) ) ! ) ;
8586 private static readonly MemberExpression FilterContextHttpContextExpr = Expression . Property ( FilterContextExpr , typeof ( RouteHandlerFilterContext ) . GetProperty ( nameof ( RouteHandlerFilterContext . HttpContext ) ) ! ) ;
@@ -166,7 +167,7 @@ private static FactoryContext CreateFactoryContext(RequestDelegateFactoryOptions
166167 RouteParameters = options ? . RouteParameterNames ? . ToList ( ) ,
167168 ThrowOnBadRequest = options ? . ThrowOnBadRequest ?? false ,
168169 DisableInferredFromBody = options ? . DisableInferBodyFromParameters ?? false ,
169- Filters = options ? . RouteHandlerFilters ? . ToList ( )
170+ Filters = options ? . RouteHandlerFilterFactories ? . ToList ( )
170171 } ;
171172
172173 private static Func < object ? , HttpContext , Task > CreateTargetableRequestDelegate ( MethodInfo methodInfo , Expression ? targetExpression , FactoryContext factoryContext )
@@ -205,7 +206,7 @@ private static FactoryContext CreateFactoryContext(RequestDelegateFactoryOptions
205206 Expression . Assign (
206207 InvokedFilterContextExpr ,
207208 Expression . New ( RouteHandlerFilterContextConstructor ,
208- new Expression [ ] { HttpContextExpr , Expression . NewArrayInit ( typeof ( object ) , factoryContext . BoxedArgs ) } ) ) ,
209+ new Expression [ ] { HttpContextExpr , RequestServicesExpr , Expression . NewArrayInit ( typeof ( object ) , factoryContext . BoxedArgs ) } ) ) ,
209210 Expression . Invoke ( invokePipeline , InvokedFilterContextExpr )
210211 ) ;
211212 }
@@ -222,13 +223,13 @@ private static FactoryContext CreateFactoryContext(RequestDelegateFactoryOptions
222223 return HandleRequestBodyAndCompileRequestDelegate ( responseWritingMethodCall , factoryContext ) ;
223224 }
224225
225- private static Func < RouteHandlerFilterContext , ValueTask < object ? > > CreateFilterPipeline ( MethodInfo methodInfo , Expression ? target , FactoryContext factoryContext )
226+ private static RouteHandlerFilterDelegate CreateFilterPipeline ( MethodInfo methodInfo , Expression ? target , FactoryContext factoryContext )
226227 {
227228 Debug . Assert ( factoryContext . Filters is not null ) ;
228229 // httpContext.Response.StatusCode >= 400
229230 // ? Task.CompletedTask
230231 // : handler((string)context.Parameters[0], (int)context.Parameters[1])
231- var filteredInvocation = Expression . Lambda < Func < RouteHandlerFilterContext , ValueTask < object ? > > > (
232+ var filteredInvocation = Expression . Lambda < RouteHandlerFilterDelegate > (
232233 Expression . Condition (
233234 Expression . GreaterThanOrEqual ( FilterContextHttpContextStatusCodeExpr , Expression . Constant ( 400 ) ) ,
234235 CompletedValueTaskExpr ,
@@ -243,9 +244,9 @@ target is null
243244
244245 for ( var i = factoryContext . Filters . Count - 1 ; i >= 0 ; i -- )
245246 {
246- var currentFilter = factoryContext . Filters ! [ i ] ;
247+ var currentFilterFactory = factoryContext . Filters ! [ i ] ;
247248 var nextFilter = filteredInvocation ;
248- filteredInvocation = ( RouteHandlerFilterContext context ) => currentFilter . InvokeAsync ( context , nextFilter ) ;
249+ filteredInvocation = ( RouteHandlerFilterContext context ) => currentFilterFactory ( methodInfo , nextFilter ) ( context ) ;
249250
250251 }
251252 return filteredInvocation ;
@@ -1693,7 +1694,7 @@ private class FactoryContext
16931694 public List < Expression > ContextArgAccess { get ; } = new ( ) ;
16941695 public Expression ? MethodCall { get ; set ; }
16951696 public List < Expression > BoxedArgs { get ; } = new ( ) ;
1696- public List < IRouteHandlerFilter > ? Filters { get ; init ; }
1697+ public List < Func < MethodInfo , RouteHandlerFilterDelegate , RouteHandlerFilterDelegate > > ? Filters { get ; init ; }
16971698 }
16981699
16991700 private static class RequestDelegateFactoryConstants
0 commit comments