@@ -62,8 +62,9 @@ public static class RequestDelegateFactory
62
62
/// Creates a <see cref="RequestDelegate"/> implementation for <paramref name="action"/>.
63
63
/// </summary>
64
64
/// <param name="action">A request handler with any number of custom parameters that often produces a response with its return value.</param>
65
+ /// <param name="serviceProvider">The <see cref="IServiceProvider"/> instance used to detect which parameters are services.</param>
65
66
/// <returns>The <see cref="RequestDelegate"/>.</returns>
66
- public static RequestDelegate Create ( Delegate action )
67
+ public static RequestDelegate Create ( Delegate action , IServiceProvider ? serviceProvider )
67
68
{
68
69
if ( action is null )
69
70
{
@@ -76,7 +77,7 @@ public static RequestDelegate Create(Delegate action)
76
77
null => null ,
77
78
} ;
78
79
79
- var targetableRequestDelegate = CreateTargetableRequestDelegate ( action . Method , targetExpression ) ;
80
+ var targetableRequestDelegate = CreateTargetableRequestDelegate ( action . Method , serviceProvider , targetExpression ) ;
80
81
81
82
return httpContext =>
82
83
{
@@ -88,15 +89,16 @@ public static RequestDelegate Create(Delegate action)
88
89
/// Creates a <see cref="RequestDelegate"/> implementation for <paramref name="methodInfo"/>.
89
90
/// </summary>
90
91
/// <param name="methodInfo">A static request handler with any number of custom parameters that often produces a response with its return value.</param>
92
+ /// <param name="serviceProvider">The <see cref="IServiceProvider"/> instance used to detect which parameters are services.</param>
91
93
/// <returns>The <see cref="RequestDelegate"/>.</returns>
92
- public static RequestDelegate Create ( MethodInfo methodInfo )
94
+ public static RequestDelegate Create ( MethodInfo methodInfo , IServiceProvider ? serviceProvider )
93
95
{
94
96
if ( methodInfo is null )
95
97
{
96
98
throw new ArgumentNullException ( nameof ( methodInfo ) ) ;
97
99
}
98
100
99
- var targetableRequestDelegate = CreateTargetableRequestDelegate ( methodInfo , targetExpression : null ) ;
101
+ var targetableRequestDelegate = CreateTargetableRequestDelegate ( methodInfo , serviceProvider , targetExpression : null ) ;
100
102
101
103
return httpContext =>
102
104
{
@@ -108,9 +110,10 @@ public static RequestDelegate Create(MethodInfo methodInfo)
108
110
/// Creates a <see cref="RequestDelegate"/> implementation for <paramref name="methodInfo"/>.
109
111
/// </summary>
110
112
/// <param name="methodInfo">A request handler with any number of custom parameters that often produces a response with its return value.</param>
113
+ /// <param name="serviceProvider">The <see cref="IServiceProvider"/> instance used to detect which parameters are services.</param>
111
114
/// <param name="targetFactory">Creates the <see langword="this"/> for the non-static method.</param>
112
115
/// <returns>The <see cref="RequestDelegate"/>.</returns>
113
- public static RequestDelegate Create ( MethodInfo methodInfo , Func < HttpContext , object > targetFactory )
116
+ public static RequestDelegate Create ( MethodInfo methodInfo , IServiceProvider ? serviceProvider , Func < HttpContext , object > targetFactory )
114
117
{
115
118
if ( methodInfo is null )
116
119
{
@@ -128,15 +131,15 @@ public static RequestDelegate Create(MethodInfo methodInfo, Func<HttpContext, ob
128
131
}
129
132
130
133
var targetExpression = Expression . Convert ( TargetExpr , methodInfo . DeclaringType ) ;
131
- var targetableRequestDelegate = CreateTargetableRequestDelegate ( methodInfo , targetExpression ) ;
134
+ var targetableRequestDelegate = CreateTargetableRequestDelegate ( methodInfo , serviceProvider , targetExpression ) ;
132
135
133
136
return httpContext =>
134
137
{
135
138
return targetableRequestDelegate ( targetFactory ( httpContext ) , httpContext ) ;
136
139
} ;
137
140
}
138
141
139
- private static Func < object ? , HttpContext , Task > CreateTargetableRequestDelegate ( MethodInfo methodInfo , Expression ? targetExpression )
142
+ private static Func < object ? , HttpContext , Task > CreateTargetableRequestDelegate ( MethodInfo methodInfo , IServiceProvider ? serviceProvider , Expression ? targetExpression )
140
143
{
141
144
// Non void return type
142
145
@@ -154,7 +157,10 @@ public static RequestDelegate Create(MethodInfo methodInfo, Func<HttpContext, ob
154
157
// return default;
155
158
// }
156
159
157
- var factoryContext = new FactoryContext ( ) ;
160
+ var factoryContext = new FactoryContext ( )
161
+ {
162
+ ServiceProvider = serviceProvider
163
+ } ;
158
164
159
165
var arguments = CreateArguments ( methodInfo . GetParameters ( ) , factoryContext ) ;
160
166
@@ -234,6 +240,15 @@ private static Expression CreateArgument(ParameterInfo parameter, FactoryContext
234
240
}
235
241
else
236
242
{
243
+ if ( factoryContext . ServiceProvider ? . GetService < IServiceProviderIsService > ( ) is IServiceProviderIsService serviceProviderIsService )
244
+ {
245
+ // If the parameter resolves as a service then get it from services
246
+ if ( serviceProviderIsService . IsService ( parameter . ParameterType ) )
247
+ {
248
+ return Expression . Call ( GetRequiredServiceMethod . MakeGenericMethod ( parameter . ParameterType ) , RequestServicesExpr ) ;
249
+ }
250
+ }
251
+
237
252
return BindParameterFromBody ( parameter . ParameterType , allowEmpty : false , factoryContext ) ;
238
253
}
239
254
}
@@ -788,6 +803,7 @@ private class FactoryContext
788
803
{
789
804
public Type ? JsonRequestBodyType { get ; set ; }
790
805
public bool AllowEmptyRequestBody { get ; set ; }
806
+ public IServiceProvider ? ServiceProvider { get ; init ; }
791
807
792
808
public bool UsingTempSourceString { get ; set ; }
793
809
public List < ( ParameterExpression , Expression ) > TryParseParams { get ; } = new ( ) ;
0 commit comments