diff --git a/src/Http/Http.Extensions/src/PublicAPI.Unshipped.txt b/src/Http/Http.Extensions/src/PublicAPI.Unshipped.txt index bdea603d4132..c940f762c584 100644 --- a/src/Http/Http.Extensions/src/PublicAPI.Unshipped.txt +++ b/src/Http/Http.Extensions/src/PublicAPI.Unshipped.txt @@ -169,9 +169,9 @@ static Microsoft.AspNetCore.Http.HeaderDictionaryTypeExtensions.AppendList(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! 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! 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! diff --git a/src/Http/Http.Extensions/src/RequestDelegateFactory.cs b/src/Http/Http.Extensions/src/RequestDelegateFactory.cs index cc761beab14c..b14ef5fb3d40 100644 --- a/src/Http/Http.Extensions/src/RequestDelegateFactory.cs +++ b/src/Http/Http.Extensions/src/RequestDelegateFactory.cs @@ -64,18 +64,13 @@ public static class RequestDelegateFactory /// A request handler with any number of custom parameters that often produces a response with its return value. /// The instance used to detect which parameters are services. /// The . - 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()), @@ -96,18 +91,13 @@ public static RequestDelegate Create(Delegate action, IServiceProvider servicePr /// A static request handler with any number of custom parameters that often produces a response with its return value. /// The instance used to detect which parameters are services. /// The . - 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 => @@ -123,18 +113,13 @@ public static RequestDelegate Create(MethodInfo methodInfo, IServiceProvider ser /// The instance used to detect which parameters are services. /// Creates the for the non-static method. /// The . - public static RequestDelegate Create(MethodInfo methodInfo, IServiceProvider serviceProvider, Func targetFactory) + public static RequestDelegate Create(MethodInfo methodInfo, IServiceProvider? serviceProvider, Func 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)); @@ -154,7 +139,7 @@ public static RequestDelegate Create(MethodInfo methodInfo, IServiceProvider ser }; } - private static Func CreateTargetableRequestDelegate(MethodInfo methodInfo, IServiceProvider serviceProvider, Expression? targetExpression) + private static Func CreateTargetableRequestDelegate(MethodInfo methodInfo, IServiceProvider? serviceProvider, Expression? targetExpression) { // Non void return type @@ -255,12 +240,10 @@ private static Expression CreateArgument(ParameterInfo parameter, FactoryContext } else { - if (factoryContext.ServiceProvider != null) + if (factoryContext.ServiceProvider?.GetService() 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); }