@@ -64,18 +64,13 @@ public static class RequestDelegateFactory
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
65
/// <param name="serviceProvider">The <see cref="IServiceProvider"/> instance used to detect which parameters are services.</param>
66
66
/// <returns>The <see cref="RequestDelegate"/>.</returns>
67
- public static RequestDelegate Create ( Delegate action , IServiceProvider serviceProvider )
67
+ public static RequestDelegate Create ( Delegate action , IServiceProvider ? serviceProvider )
68
68
{
69
69
if ( action is null )
70
70
{
71
71
throw new ArgumentNullException ( nameof ( action ) ) ;
72
72
}
73
73
74
- if ( serviceProvider is null )
75
- {
76
- throw new ArgumentNullException ( nameof ( serviceProvider ) ) ;
77
- }
78
-
79
74
var targetExpression = action . Target switch
80
75
{
81
76
object => Expression . Convert ( TargetExpr , action . Target . GetType ( ) ) ,
@@ -96,18 +91,13 @@ public static RequestDelegate Create(Delegate action, IServiceProvider servicePr
96
91
/// <param name="methodInfo">A static request handler with any number of custom parameters that often produces a response with its return value.</param>
97
92
/// <param name="serviceProvider">The <see cref="IServiceProvider"/> instance used to detect which parameters are services.</param>
98
93
/// <returns>The <see cref="RequestDelegate"/>.</returns>
99
- public static RequestDelegate Create ( MethodInfo methodInfo , IServiceProvider serviceProvider )
94
+ public static RequestDelegate Create ( MethodInfo methodInfo , IServiceProvider ? serviceProvider )
100
95
{
101
96
if ( methodInfo is null )
102
97
{
103
98
throw new ArgumentNullException ( nameof ( methodInfo ) ) ;
104
99
}
105
100
106
- if ( serviceProvider is null )
107
- {
108
- throw new ArgumentNullException ( nameof ( serviceProvider ) ) ;
109
- }
110
-
111
101
var targetableRequestDelegate = CreateTargetableRequestDelegate ( methodInfo , serviceProvider , targetExpression : null ) ;
112
102
113
103
return httpContext =>
@@ -123,18 +113,13 @@ public static RequestDelegate Create(MethodInfo methodInfo, IServiceProvider ser
123
113
/// <param name="serviceProvider">The <see cref="IServiceProvider"/> instance used to detect which parameters are services.</param>
124
114
/// <param name="targetFactory">Creates the <see langword="this"/> for the non-static method.</param>
125
115
/// <returns>The <see cref="RequestDelegate"/>.</returns>
126
- public static RequestDelegate Create ( MethodInfo methodInfo , IServiceProvider serviceProvider , Func < HttpContext , object > targetFactory )
116
+ public static RequestDelegate Create ( MethodInfo methodInfo , IServiceProvider ? serviceProvider , Func < HttpContext , object > targetFactory )
127
117
{
128
118
if ( methodInfo is null )
129
119
{
130
120
throw new ArgumentNullException ( nameof ( methodInfo ) ) ;
131
121
}
132
122
133
- if ( serviceProvider is null )
134
- {
135
- throw new ArgumentNullException ( nameof ( serviceProvider ) ) ;
136
- }
137
-
138
123
if ( targetFactory is null )
139
124
{
140
125
throw new ArgumentNullException ( nameof ( targetFactory ) ) ;
@@ -154,7 +139,7 @@ public static RequestDelegate Create(MethodInfo methodInfo, IServiceProvider ser
154
139
} ;
155
140
}
156
141
157
- private static Func < object ? , HttpContext , Task > CreateTargetableRequestDelegate ( MethodInfo methodInfo , IServiceProvider serviceProvider , Expression ? targetExpression )
142
+ private static Func < object ? , HttpContext , Task > CreateTargetableRequestDelegate ( MethodInfo methodInfo , IServiceProvider ? serviceProvider , Expression ? targetExpression )
158
143
{
159
144
// Non void return type
160
145
@@ -255,12 +240,10 @@ private static Expression CreateArgument(ParameterInfo parameter, FactoryContext
255
240
}
256
241
else
257
242
{
258
- if ( factoryContext . ServiceProvider != null )
243
+ if ( factoryContext . ServiceProvider ? . GetService < IServiceProviderIsService > ( ) is IServiceProviderIsService serviceProviderIsService )
259
244
{
260
- using var scope = factoryContext . ServiceProvider . CreateScope ( ) ;
261
-
262
245
// If the parameter resolves as a service then get it from services
263
- if ( scope . ServiceProvider . GetService ( parameter . ParameterType ) is not null )
246
+ if ( serviceProviderIsService . IsService ( parameter . ParameterType ) )
264
247
{
265
248
return Expression . Call ( GetRequiredServiceMethod . MakeGenericMethod ( parameter . ParameterType ) , RequestServicesExpr ) ;
266
249
}
0 commit comments