Skip to content

Commit 432b02a

Browse files
committed
Use IServiceProviderIsService to detect if a parameter is a service.
1 parent 28e3db8 commit 432b02a

File tree

2 files changed

+9
-26
lines changed

2 files changed

+9
-26
lines changed

src/Http/Http.Extensions/src/PublicAPI.Unshipped.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,9 @@ static Microsoft.AspNetCore.Http.HeaderDictionaryTypeExtensions.AppendList<T>(th
169169
static Microsoft.AspNetCore.Http.HeaderDictionaryTypeExtensions.GetTypedHeaders(this Microsoft.AspNetCore.Http.HttpRequest! request) -> Microsoft.AspNetCore.Http.Headers.RequestHeaders!
170170
static Microsoft.AspNetCore.Http.HeaderDictionaryTypeExtensions.GetTypedHeaders(this Microsoft.AspNetCore.Http.HttpResponse! response) -> Microsoft.AspNetCore.Http.Headers.ResponseHeaders!
171171
static Microsoft.AspNetCore.Http.HttpContextServerVariableExtensions.GetServerVariable(this Microsoft.AspNetCore.Http.HttpContext! context, string! variableName) -> string?
172-
static Microsoft.AspNetCore.Http.RequestDelegateFactory.Create(System.Delegate! action, System.IServiceProvider! serviceProvider) -> Microsoft.AspNetCore.Http.RequestDelegate!
173-
static Microsoft.AspNetCore.Http.RequestDelegateFactory.Create(System.Reflection.MethodInfo! methodInfo, System.IServiceProvider! serviceProvider) -> Microsoft.AspNetCore.Http.RequestDelegate!
174-
static Microsoft.AspNetCore.Http.RequestDelegateFactory.Create(System.Reflection.MethodInfo! methodInfo, System.IServiceProvider! serviceProvider, System.Func<Microsoft.AspNetCore.Http.HttpContext!, object!>! targetFactory) -> Microsoft.AspNetCore.Http.RequestDelegate!
172+
static Microsoft.AspNetCore.Http.RequestDelegateFactory.Create(System.Delegate! action, System.IServiceProvider? serviceProvider) -> Microsoft.AspNetCore.Http.RequestDelegate!
173+
static Microsoft.AspNetCore.Http.RequestDelegateFactory.Create(System.Reflection.MethodInfo! methodInfo, System.IServiceProvider? serviceProvider) -> Microsoft.AspNetCore.Http.RequestDelegate!
174+
static Microsoft.AspNetCore.Http.RequestDelegateFactory.Create(System.Reflection.MethodInfo! methodInfo, System.IServiceProvider? serviceProvider, System.Func<Microsoft.AspNetCore.Http.HttpContext!, object!>! targetFactory) -> Microsoft.AspNetCore.Http.RequestDelegate!
175175
static Microsoft.AspNetCore.Http.ResponseExtensions.Clear(this Microsoft.AspNetCore.Http.HttpResponse! response) -> void
176176
static Microsoft.AspNetCore.Http.ResponseExtensions.Redirect(this Microsoft.AspNetCore.Http.HttpResponse! response, string! location, bool permanent, bool preserveMethod) -> void
177177
static Microsoft.AspNetCore.Http.SendFileResponseExtensions.SendFileAsync(this Microsoft.AspNetCore.Http.HttpResponse! response, Microsoft.Extensions.FileProviders.IFileInfo! file, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task!

src/Http/Http.Extensions/src/RequestDelegateFactory.cs

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,13 @@ public static class RequestDelegateFactory
6464
/// <param name="action">A request handler with any number of custom parameters that often produces a response with its return value.</param>
6565
/// <param name="serviceProvider">The <see cref="IServiceProvider"/> instance used to detect which parameters are services.</param>
6666
/// <returns>The <see cref="RequestDelegate"/>.</returns>
67-
public static RequestDelegate Create(Delegate action, IServiceProvider serviceProvider)
67+
public static RequestDelegate Create(Delegate action, IServiceProvider? serviceProvider)
6868
{
6969
if (action is null)
7070
{
7171
throw new ArgumentNullException(nameof(action));
7272
}
7373

74-
if (serviceProvider is null)
75-
{
76-
throw new ArgumentNullException(nameof(serviceProvider));
77-
}
78-
7974
var targetExpression = action.Target switch
8075
{
8176
object => Expression.Convert(TargetExpr, action.Target.GetType()),
@@ -96,18 +91,13 @@ public static RequestDelegate Create(Delegate action, IServiceProvider servicePr
9691
/// <param name="methodInfo">A static request handler with any number of custom parameters that often produces a response with its return value.</param>
9792
/// <param name="serviceProvider">The <see cref="IServiceProvider"/> instance used to detect which parameters are services.</param>
9893
/// <returns>The <see cref="RequestDelegate"/>.</returns>
99-
public static RequestDelegate Create(MethodInfo methodInfo, IServiceProvider serviceProvider)
94+
public static RequestDelegate Create(MethodInfo methodInfo, IServiceProvider? serviceProvider)
10095
{
10196
if (methodInfo is null)
10297
{
10398
throw new ArgumentNullException(nameof(methodInfo));
10499
}
105100

106-
if (serviceProvider is null)
107-
{
108-
throw new ArgumentNullException(nameof(serviceProvider));
109-
}
110-
111101
var targetableRequestDelegate = CreateTargetableRequestDelegate(methodInfo, serviceProvider, targetExpression: null);
112102

113103
return httpContext =>
@@ -123,18 +113,13 @@ public static RequestDelegate Create(MethodInfo methodInfo, IServiceProvider ser
123113
/// <param name="serviceProvider">The <see cref="IServiceProvider"/> instance used to detect which parameters are services.</param>
124114
/// <param name="targetFactory">Creates the <see langword="this"/> for the non-static method.</param>
125115
/// <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)
127117
{
128118
if (methodInfo is null)
129119
{
130120
throw new ArgumentNullException(nameof(methodInfo));
131121
}
132122

133-
if (serviceProvider is null)
134-
{
135-
throw new ArgumentNullException(nameof(serviceProvider));
136-
}
137-
138123
if (targetFactory is null)
139124
{
140125
throw new ArgumentNullException(nameof(targetFactory));
@@ -154,7 +139,7 @@ public static RequestDelegate Create(MethodInfo methodInfo, IServiceProvider ser
154139
};
155140
}
156141

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)
158143
{
159144
// Non void return type
160145

@@ -255,12 +240,10 @@ private static Expression CreateArgument(ParameterInfo parameter, FactoryContext
255240
}
256241
else
257242
{
258-
if (factoryContext.ServiceProvider != null)
243+
if (factoryContext.ServiceProvider?.GetService<IServiceProviderIsService>() is IServiceProviderIsService serviceProviderIsService)
259244
{
260-
using var scope = factoryContext.ServiceProvider.CreateScope();
261-
262245
// 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))
264247
{
265248
return Expression.Call(GetRequiredServiceMethod.MakeGenericMethod(parameter.ParameterType), RequestServicesExpr);
266249
}

0 commit comments

Comments
 (0)