Skip to content

Commit

Permalink
Use IServiceProviderIsService to detect if a parameter is a service.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidfowl committed Jun 12, 2021
1 parent 28e3db8 commit 432b02a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 26 deletions.
6 changes: 3 additions & 3 deletions src/Http/Http.Extensions/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@ static Microsoft.AspNetCore.Http.HeaderDictionaryTypeExtensions.AppendList<T>(th
static Microsoft.AspNetCore.Http.HeaderDictionaryTypeExtensions.GetTypedHeaders(this Microsoft.AspNetCore.Http.HttpRequest! request) -> Microsoft.AspNetCore.Http.Headers.RequestHeaders!
static Microsoft.AspNetCore.Http.HeaderDictionaryTypeExtensions.GetTypedHeaders(this Microsoft.AspNetCore.Http.HttpResponse! response) -> Microsoft.AspNetCore.Http.Headers.ResponseHeaders!
static Microsoft.AspNetCore.Http.HttpContextServerVariableExtensions.GetServerVariable(this Microsoft.AspNetCore.Http.HttpContext! context, string! variableName) -> string?
static Microsoft.AspNetCore.Http.RequestDelegateFactory.Create(System.Delegate! action, System.IServiceProvider! serviceProvider) -> Microsoft.AspNetCore.Http.RequestDelegate!
static Microsoft.AspNetCore.Http.RequestDelegateFactory.Create(System.Reflection.MethodInfo! methodInfo, System.IServiceProvider! serviceProvider) -> Microsoft.AspNetCore.Http.RequestDelegate!
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!
static Microsoft.AspNetCore.Http.RequestDelegateFactory.Create(System.Delegate! action, System.IServiceProvider? serviceProvider) -> Microsoft.AspNetCore.Http.RequestDelegate!
static Microsoft.AspNetCore.Http.RequestDelegateFactory.Create(System.Reflection.MethodInfo! methodInfo, System.IServiceProvider? serviceProvider) -> Microsoft.AspNetCore.Http.RequestDelegate!
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!
static Microsoft.AspNetCore.Http.ResponseExtensions.Clear(this Microsoft.AspNetCore.Http.HttpResponse! response) -> void
static Microsoft.AspNetCore.Http.ResponseExtensions.Redirect(this Microsoft.AspNetCore.Http.HttpResponse! response, string! location, bool permanent, bool preserveMethod) -> void
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!
Expand Down
29 changes: 6 additions & 23 deletions src/Http/Http.Extensions/src/RequestDelegateFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,13 @@ public static class RequestDelegateFactory
/// <param name="action">A request handler with any number of custom parameters that often produces a response with its return value.</param>
/// <param name="serviceProvider">The <see cref="IServiceProvider"/> instance used to detect which parameters are services.</param>
/// <returns>The <see cref="RequestDelegate"/>.</returns>
public static RequestDelegate Create(Delegate action, IServiceProvider serviceProvider)
public static RequestDelegate Create(Delegate action, IServiceProvider? serviceProvider)
{
if (action is null)
{
throw new ArgumentNullException(nameof(action));
}

if (serviceProvider is null)
{
throw new ArgumentNullException(nameof(serviceProvider));
}

var targetExpression = action.Target switch
{
object => Expression.Convert(TargetExpr, action.Target.GetType()),
Expand All @@ -96,18 +91,13 @@ public static RequestDelegate Create(Delegate action, IServiceProvider servicePr
/// <param name="methodInfo">A static request handler with any number of custom parameters that often produces a response with its return value.</param>
/// <param name="serviceProvider">The <see cref="IServiceProvider"/> instance used to detect which parameters are services.</param>
/// <returns>The <see cref="RequestDelegate"/>.</returns>
public static RequestDelegate Create(MethodInfo methodInfo, IServiceProvider serviceProvider)
public static RequestDelegate Create(MethodInfo methodInfo, IServiceProvider? serviceProvider)
{
if (methodInfo is null)
{
throw new ArgumentNullException(nameof(methodInfo));
}

if (serviceProvider is null)
{
throw new ArgumentNullException(nameof(serviceProvider));
}

var targetableRequestDelegate = CreateTargetableRequestDelegate(methodInfo, serviceProvider, targetExpression: null);

return httpContext =>
Expand All @@ -123,18 +113,13 @@ public static RequestDelegate Create(MethodInfo methodInfo, IServiceProvider ser
/// <param name="serviceProvider">The <see cref="IServiceProvider"/> instance used to detect which parameters are services.</param>
/// <param name="targetFactory">Creates the <see langword="this"/> for the non-static method.</param>
/// <returns>The <see cref="RequestDelegate"/>.</returns>
public static RequestDelegate Create(MethodInfo methodInfo, IServiceProvider serviceProvider, Func<HttpContext, object> targetFactory)
public static RequestDelegate Create(MethodInfo methodInfo, IServiceProvider? serviceProvider, Func<HttpContext, object> targetFactory)
{
if (methodInfo is null)
{
throw new ArgumentNullException(nameof(methodInfo));
}

if (serviceProvider is null)
{
throw new ArgumentNullException(nameof(serviceProvider));
}

if (targetFactory is null)
{
throw new ArgumentNullException(nameof(targetFactory));
Expand All @@ -154,7 +139,7 @@ public static RequestDelegate Create(MethodInfo methodInfo, IServiceProvider ser
};
}

private static Func<object?, HttpContext, Task> CreateTargetableRequestDelegate(MethodInfo methodInfo, IServiceProvider serviceProvider, Expression? targetExpression)
private static Func<object?, HttpContext, Task> CreateTargetableRequestDelegate(MethodInfo methodInfo, IServiceProvider? serviceProvider, Expression? targetExpression)
{
// Non void return type

Expand Down Expand Up @@ -255,12 +240,10 @@ private static Expression CreateArgument(ParameterInfo parameter, FactoryContext
}
else
{
if (factoryContext.ServiceProvider != null)
if (factoryContext.ServiceProvider?.GetService<IServiceProviderIsService>() is IServiceProviderIsService serviceProviderIsService)
{
using var scope = factoryContext.ServiceProvider.CreateScope();

// If the parameter resolves as a service then get it from services
if (scope.ServiceProvider.GetService(parameter.ParameterType) is not null)
if (serviceProviderIsService.IsService(parameter.ParameterType))
{
return Expression.Call(GetRequiredServiceMethod.MakeGenericMethod(parameter.ParameterType), RequestServicesExpr);
}
Expand Down

0 comments on commit 432b02a

Please sign in to comment.