@@ -121,7 +121,9 @@ public static RequestDelegateResult Create(Delegate handler, RequestDelegateFact
121
121
122
122
var factoryContext = CreateFactoryContext ( options ) ;
123
123
124
- var targetableRequestDelegate = CreateTargetableRequestDelegate ( handler . Method , targetExpression , factoryContext ) ;
124
+ Expression < Func < HttpContext , object ? > > targetFactory = ( httpContext ) => handler . Target ;
125
+
126
+ var targetableRequestDelegate = CreateTargetableRequestDelegate ( handler . Method , targetExpression , factoryContext , targetFactory ) ;
125
127
126
128
return new RequestDelegateResult ( httpContext => targetableRequestDelegate ( handler . Target , httpContext ) , factoryContext . Metadata ) ;
127
129
}
@@ -162,7 +164,7 @@ public static RequestDelegateResult Create(MethodInfo methodInfo, Func<HttpConte
162
164
}
163
165
164
166
var targetExpression = Expression . Convert ( TargetExpr , methodInfo . DeclaringType ) ;
165
- var targetableRequestDelegate = CreateTargetableRequestDelegate ( methodInfo , targetExpression , factoryContext ) ;
167
+ var targetableRequestDelegate = CreateTargetableRequestDelegate ( methodInfo , targetExpression , factoryContext , context => targetFactory ) ;
166
168
167
169
return new RequestDelegateResult ( httpContext => targetableRequestDelegate ( targetFactory ( httpContext ) , httpContext ) , factoryContext . Metadata ) ;
168
170
}
@@ -187,7 +189,7 @@ private static FactoryContext CreateFactoryContext(RequestDelegateFactoryOptions
187
189
return context ;
188
190
}
189
191
190
- private static Func < object ? , HttpContext , Task > CreateTargetableRequestDelegate ( MethodInfo methodInfo , Expression ? targetExpression , FactoryContext factoryContext )
192
+ private static Func < object ? , HttpContext , Task > CreateTargetableRequestDelegate ( MethodInfo methodInfo , Expression ? targetExpression , FactoryContext factoryContext , Expression < Func < HttpContext , object ? > > ? targetFactory = null )
191
193
{
192
194
// Non void return type
193
195
@@ -223,7 +225,7 @@ private static FactoryContext CreateFactoryContext(RequestDelegateFactoryOptions
223
225
// return type associated with the request to allow for the filter invocation pipeline.
224
226
if ( factoryContext . Filters is { Count : > 0 } )
225
227
{
226
- var filterPipeline = CreateFilterPipeline ( methodInfo , targetExpression , factoryContext ) ;
228
+ var filterPipeline = CreateFilterPipeline ( methodInfo , targetExpression , factoryContext , targetFactory ) ;
227
229
Expression < Func < RouteHandlerInvocationContext , ValueTask < object ? > > > invokePipeline = ( context ) => filterPipeline ( context ) ;
228
230
returnType = typeof ( ValueTask < object ? > ) ;
229
231
// var filterContext = new RouteHandlerInvocationContext(httpContext, new[] { (object)name_local, (object)int_local });
@@ -250,22 +252,28 @@ private static FactoryContext CreateFactoryContext(RequestDelegateFactoryOptions
250
252
return HandleRequestBodyAndCompileRequestDelegate ( responseWritingMethodCall , factoryContext ) ;
251
253
}
252
254
253
- private static RouteHandlerFilterDelegate CreateFilterPipeline ( MethodInfo methodInfo , Expression ? target , FactoryContext factoryContext )
255
+ private static RouteHandlerFilterDelegate CreateFilterPipeline ( MethodInfo methodInfo , Expression ? targetExpression , FactoryContext factoryContext , Expression < Func < HttpContext , object ? > > ? targetFactory )
254
256
{
255
257
Debug . Assert ( factoryContext . Filters is not null ) ;
256
258
// httpContext.Response.StatusCode >= 400
257
259
// ? Task.CompletedTask
258
- // : handler((string)context.Parameters[0], (int)context.Parameters[1])
260
+ // : {
261
+ // target = targetFactory(httpContext);
262
+ // handler((string)context.Parameters[0], (int)context.Parameters[1]);
263
+ // }
259
264
var filteredInvocation = Expression . Lambda < RouteHandlerFilterDelegate > (
260
265
Expression . Condition (
261
266
Expression . GreaterThanOrEqual ( FilterContextHttpContextStatusCodeExpr , Expression . Constant ( 400 ) ) ,
262
267
CompletedValueTaskExpr ,
263
268
Expression . Block (
264
- new [ ] { TargetExpr } ,
269
+ new [ ] { TargetExpr , HttpContextExpr } ,
270
+ targetFactory == null
271
+ ? Expression . Empty ( )
272
+ : Expression . Assign ( TargetExpr , Expression . Invoke ( targetFactory , HttpContextExpr ) ) ,
265
273
Expression . Call ( WrapObjectAsValueTaskMethod ,
266
- target is null
274
+ targetExpression is null
267
275
? Expression . Call ( methodInfo , factoryContext . ContextArgAccess )
268
- : Expression . Call ( target , methodInfo , factoryContext . ContextArgAccess ) )
276
+ : Expression . Call ( targetExpression , methodInfo , factoryContext . ContextArgAccess ) )
269
277
) ) ,
270
278
FilterContextExpr ) . Compile ( ) ;
271
279
var routeHandlerContext = new RouteHandlerContext (
0 commit comments