diff --git a/src/Http/Http.Abstractions/src/Extensions/MapMiddleware.cs b/src/Http/Http.Abstractions/src/Extensions/MapMiddleware.cs
index cf9e617bd8cb..64fcc157b689 100644
--- a/src/Http/Http.Abstractions/src/Extensions/MapMiddleware.cs
+++ b/src/Http/Http.Abstractions/src/Extensions/MapMiddleware.cs
@@ -46,7 +46,7 @@ public MapMiddleware(RequestDelegate next, MapOptions options)
///
/// The for the current request.
/// A task that represents the execution of this middleware.
- public async Task Invoke(HttpContext context)
+ public Task Invoke(HttpContext context)
{
if (context == null)
{
@@ -55,32 +55,32 @@ public async Task Invoke(HttpContext context)
if (context.Request.Path.StartsWithSegments(_options.PathMatch, out var matchedPath, out var remainingPath))
{
- var path = context.Request.Path;
- var pathBase = context.Request.PathBase;
-
if (!_options.PreserveMatchedPathSegment)
{
- // Update the path
- context.Request.PathBase = pathBase.Add(matchedPath);
- context.Request.Path = remainingPath;
+ return InvokeCore(context, matchedPath, remainingPath);
}
+ return _options.Branch!(context);
+ }
+ return _next(context);
+ }
- try
- {
- await _options.Branch!(context);
- }
- finally
- {
- if (!_options.PreserveMatchedPathSegment)
- {
- context.Request.PathBase = pathBase;
- context.Request.Path = path;
- }
- }
+ private async Task InvokeCore(HttpContext context, string matchedPath, string remainingPath)
+ {
+ var path = context.Request.Path;
+ var pathBase = context.Request.PathBase;
+
+ // Update the path
+ context.Request.PathBase = pathBase.Add(matchedPath);
+ context.Request.Path = remainingPath;
+
+ try
+ {
+ await _options.Branch!(context);
}
- else
+ finally
{
- await _next(context);
+ context.Request.PathBase = pathBase;
+ context.Request.Path = path;
}
}
}
diff --git a/src/Http/Http.Abstractions/src/Extensions/MapWhenMiddleware.cs b/src/Http/Http.Abstractions/src/Extensions/MapWhenMiddleware.cs
index 4c384104e61e..8f7cd24dbead 100644
--- a/src/Http/Http.Abstractions/src/Extensions/MapWhenMiddleware.cs
+++ b/src/Http/Http.Abstractions/src/Extensions/MapWhenMiddleware.cs
@@ -51,7 +51,7 @@ public MapWhenMiddleware(RequestDelegate next, MapWhenOptions options)
///
/// The for the current request.
/// A task that represents the execution of this middleware.
- public async Task Invoke(HttpContext context)
+ public Task Invoke(HttpContext context)
{
if (context == null)
{
@@ -60,12 +60,9 @@ public async Task Invoke(HttpContext context)
if (_options.Predicate!(context))
{
- await _options.Branch!(context);
- }
- else
- {
- await _next(context);
+ return _options.Branch!(context);
}
+ return _next(context);
}
}
}
diff --git a/src/Http/Http.Abstractions/src/Extensions/UsePathBaseMiddleware.cs b/src/Http/Http.Abstractions/src/Extensions/UsePathBaseMiddleware.cs
index 103bdd9601f4..adff7b163804 100644
--- a/src/Http/Http.Abstractions/src/Extensions/UsePathBaseMiddleware.cs
+++ b/src/Http/Http.Abstractions/src/Extensions/UsePathBaseMiddleware.cs
@@ -1,4 +1,4 @@
-// Copyright (c) .NET Foundation. All rights reserved.
+// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
@@ -41,37 +41,36 @@ public UsePathBaseMiddleware(RequestDelegate next, PathString pathBase)
///
/// The for the current request.
/// A task that represents the execution of this middleware.
- public async Task Invoke(HttpContext context)
+ public Task Invoke(HttpContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
- PathString matchedPath;
- PathString remainingPath;
-
- if (context.Request.Path.StartsWithSegments(_pathBase, out matchedPath, out remainingPath))
+ if (context.Request.Path.StartsWithSegments(_pathBase, out var matchedPath, out var remainingPath))
{
- var originalPath = context.Request.Path;
- var originalPathBase = context.Request.PathBase;
- context.Request.Path = remainingPath;
- context.Request.PathBase = originalPathBase.Add(matchedPath);
-
- try
- {
- await _next(context);
- }
- finally
- {
- context.Request.Path = originalPath;
- context.Request.PathBase = originalPathBase;
- }
+ return InvokeCore(context, matchedPath, remainingPath);
}
- else
+ return _next(context);
+ }
+
+ private async Task InvokeCore(HttpContext context, string matchedPath, string remainingPath)
+ {
+ var originalPath = context.Request.Path;
+ var originalPathBase = context.Request.PathBase;
+ context.Request.Path = remainingPath;
+ context.Request.PathBase = originalPathBase.Add(matchedPath);
+
+ try
{
await _next(context);
}
+ finally
+ {
+ context.Request.Path = originalPath;
+ context.Request.PathBase = originalPathBase;
+ }
}
}
-}
\ No newline at end of file
+}
diff --git a/src/Middleware/Diagnostics.EntityFrameworkCore/src/MigrationsEndPointMiddleware.cs b/src/Middleware/Diagnostics.EntityFrameworkCore/src/MigrationsEndPointMiddleware.cs
index 458b99e37245..b0b19f0a4427 100644
--- a/src/Middleware/Diagnostics.EntityFrameworkCore/src/MigrationsEndPointMiddleware.cs
+++ b/src/Middleware/Diagnostics.EntityFrameworkCore/src/MigrationsEndPointMiddleware.cs
@@ -59,7 +59,7 @@ public MigrationsEndPointMiddleware(
///
/// The context for the current request.
/// A task that represents the asynchronous operation.
- public virtual async Task Invoke(HttpContext context)
+ public virtual Task Invoke(HttpContext context)
{
if (context == null)
{
@@ -68,39 +68,41 @@ public virtual async Task Invoke(HttpContext context)
if (context.Request.Path.Equals(_options.Path))
{
- _logger.RequestPathMatched(context.Request.Path);
+ return InvokeCore(context);
+ }
+ return _next(context);
+ }
- var db = await GetDbContext(context, _logger);
+ private async Task InvokeCore(HttpContext context)
+ {
+ _logger.RequestPathMatched(context.Request.Path);
- if (db != null)
+ var db = await GetDbContext(context, _logger);
+
+ if (db != null)
+ {
+ var dbName = db.GetType().FullName!;
+ try
{
- var dbName = db.GetType().FullName!;
- try
- {
- _logger.ApplyingMigrations(dbName);
+ _logger.ApplyingMigrations(dbName);
- await db.Database.MigrateAsync();
+ await db.Database.MigrateAsync();
- context.Response.StatusCode = (int)HttpStatusCode.NoContent;
- context.Response.Headers.Add("Pragma", new[] { "no-cache" });
- context.Response.Headers.Add("Cache-Control", new[] { "no-cache,no-store" });
+ context.Response.StatusCode = (int)HttpStatusCode.NoContent;
+ context.Response.Headers.Add("Pragma", new[] { "no-cache" });
+ context.Response.Headers.Add("Cache-Control", new[] { "no-cache,no-store" });
- _logger.MigrationsApplied(dbName);
- }
- catch (Exception ex)
- {
- var message = Strings.FormatMigrationsEndPointMiddleware_Exception(dbName) + ex;
+ _logger.MigrationsApplied(dbName);
+ }
+ catch (Exception ex)
+ {
+ var message = Strings.FormatMigrationsEndPointMiddleware_Exception(dbName) + ex;
- _logger.MigrationsEndPointMiddlewareException(dbName, ex);
+ _logger.MigrationsEndPointMiddlewareException(dbName, ex);
- throw new InvalidOperationException(message, ex);
- }
+ throw new InvalidOperationException(message, ex);
}
}
- else
- {
- await _next(context);
- }
}
private static async Task GetDbContext(HttpContext context, ILogger logger)
diff --git a/src/Middleware/HttpOverrides/src/HttpMethodOverrideMiddleware.cs b/src/Middleware/HttpOverrides/src/HttpMethodOverrideMiddleware.cs
index 9d44412cc747..45c458363844 100644
--- a/src/Middleware/HttpOverrides/src/HttpMethodOverrideMiddleware.cs
+++ b/src/Middleware/HttpOverrides/src/HttpMethodOverrideMiddleware.cs
@@ -41,7 +41,7 @@ public HttpMethodOverrideMiddleware(RequestDelegate next, IOptions
/// The for the current request.
- public async Task Invoke(HttpContext context)
+ public Task Invoke(HttpContext context)
{
if (HttpMethods.IsPost(context.Request.Method))
{
@@ -49,12 +49,7 @@ public async Task Invoke(HttpContext context)
{
if (context.Request.HasFormContentType)
{
- var form = await context.Request.ReadFormAsync();
- var methodType = form[_options.FormFieldName];
- if (!string.IsNullOrEmpty(methodType))
- {
- context.Request.Method = methodType;
- }
+ return InvokeCore(context);
}
}
else
@@ -66,6 +61,17 @@ public async Task Invoke(HttpContext context)
}
}
}
+ return _next(context);
+ }
+
+ private async Task InvokeCore(HttpContext context)
+ {
+ var form = await context.Request.ReadFormAsync();
+ var methodType = form[_options.FormFieldName!];
+ if (!string.IsNullOrEmpty(methodType))
+ {
+ context.Request.Method = methodType;
+ }
await _next(context);
}
}
diff --git a/src/Middleware/ResponseCompression/src/ResponseCompressionMiddleware.cs b/src/Middleware/ResponseCompression/src/ResponseCompressionMiddleware.cs
index e37b39dde7d2..a68307574f4c 100644
--- a/src/Middleware/ResponseCompression/src/ResponseCompressionMiddleware.cs
+++ b/src/Middleware/ResponseCompression/src/ResponseCompressionMiddleware.cs
@@ -44,14 +44,17 @@ public ResponseCompressionMiddleware(RequestDelegate next, IResponseCompressionP
///
/// The .
/// A task that represents the execution of this middleware.
- public async Task Invoke(HttpContext context)
+ public Task Invoke(HttpContext context)
{
if (!_provider.CheckRequestAcceptsCompression(context))
{
- await _next(context);
- return;
+ return _next(context);
}
+ return InvokeCore(context);
+ }
+ private async Task InvokeCore(HttpContext context)
+ {
var originalBodyFeature = context.Features.Get();
var originalCompressionFeature = context.Features.Get();
diff --git a/src/Middleware/Spa/SpaProxy/src/SpaProxyMiddleware.cs b/src/Middleware/Spa/SpaProxy/src/SpaProxyMiddleware.cs
index b230f0d1dd90..da2ea951a1b2 100644
--- a/src/Middleware/Spa/SpaProxy/src/SpaProxyMiddleware.cs
+++ b/src/Middleware/Spa/SpaProxy/src/SpaProxyMiddleware.cs
@@ -45,29 +45,31 @@ public SpaProxyMiddleware(
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
- public async Task Invoke(HttpContext context)
+ public Task Invoke(HttpContext context)
{
if (context.Request.Path.Equals(new Uri(_options.Value.ServerUrl).LocalPath))
{
- context.Response.Headers[HeaderNames.CacheControl] = "no-cache, no-store, must-revalidate, max-age=0";
- if (!await _spaProxyLaunchManager.IsSpaProxyRunning(context.RequestAborted))
- {
- _spaProxyLaunchManager.StartInBackground(_hostLifetime.ApplicationStopping);
- _logger.LogInformation("SPA proxy is not ready. Returning temporary landing page.");
- context.Response.ContentType = "text/html";
+ return InvokeCore(context);
+ }
+ return _next(context);
+ }
+
+ private async Task InvokeCore(HttpContext context)
+ {
+ context.Response.Headers[HeaderNames.CacheControl] = "no-cache, no-store, must-revalidate, max-age=0";
+ if (!await _spaProxyLaunchManager.IsSpaProxyRunning(context.RequestAborted))
+ {
+ _spaProxyLaunchManager.StartInBackground(_hostLifetime.ApplicationStopping);
+ _logger.LogInformation("SPA proxy is not ready. Returning temporary landing page.");
+ context.Response.ContentType = "text/html";
- await using var writer = new StreamWriter(context.Response.Body, Encoding.UTF8);
- await writer.WriteAsync(GenerateSpaLaunchPage(_options.Value));
- }
- else
- {
- _logger.LogInformation($"SPA proxy is ready. Redirecting to {_options.Value.ServerUrl}.");
- context.Response.Redirect(_options.Value.ServerUrl);
- }
+ await using var writer = new StreamWriter(context.Response.Body, Encoding.UTF8);
+ await writer.WriteAsync(GenerateSpaLaunchPage(_options.Value));
}
else
{
- await _next(context);
+ _logger.LogInformation($"SPA proxy is ready. Redirecting to {_options.Value.ServerUrl}.");
+ context.Response.Redirect(_options.Value.ServerUrl);
}
string GenerateSpaLaunchPage(SpaDevelopmentServerOptions options)
diff --git a/src/Middleware/Spa/SpaServices.Extensions/src/Proxying/ConditionalProxyMiddleware.cs b/src/Middleware/Spa/SpaServices.Extensions/src/Proxying/ConditionalProxyMiddleware.cs
index d1bb2d36986c..8915a308a0eb 100644
--- a/src/Middleware/Spa/SpaServices.Extensions/src/Proxying/ConditionalProxyMiddleware.cs
+++ b/src/Middleware/Spa/SpaServices.Extensions/src/Proxying/ConditionalProxyMiddleware.cs
@@ -43,16 +43,22 @@ public ConditionalProxyMiddleware(
_applicationStoppingToken = applicationLifetime.ApplicationStopping;
}
- public async Task Invoke(HttpContext context)
+ public Task Invoke(HttpContext context)
{
if (context.Request.Path.StartsWithSegments(_pathPrefix) || _pathPrefixIsRoot)
{
- var didProxyRequest = await SpaProxy.PerformProxyRequest(
- context, _httpClient, _baseUriTask, _applicationStoppingToken, proxy404s: false);
- if (didProxyRequest)
- {
- return;
- }
+ return InvokeCore(context);
+ }
+ return _next.Invoke(context);
+ }
+
+ private async Task InvokeCore(HttpContext context)
+ {
+ var didProxyRequest = await SpaProxy.PerformProxyRequest(
+ context, _httpClient, _baseUriTask, _applicationStoppingToken, proxy404s: false);
+ if (didProxyRequest)
+ {
+ return;
}
// Not a request we can proxy
diff --git a/src/Servers/IIS/IISIntegration/src/IISMiddleware.cs b/src/Servers/IIS/IISIntegration/src/IISMiddleware.cs
index caae96f06a8d..d2cd5d1698ee 100644
--- a/src/Servers/IIS/IISIntegration/src/IISMiddleware.cs
+++ b/src/Servers/IIS/IISIntegration/src/IISMiddleware.cs
@@ -115,13 +115,13 @@ public IISMiddleware(RequestDelegate next,
///
/// The .
/// A that represents the asynchronous operation.
- public async Task Invoke(HttpContext httpContext)
+ public Task Invoke(HttpContext httpContext)
{
if (!string.Equals(_pairingToken, httpContext.Request.Headers[MSAspNetCoreToken], StringComparison.Ordinal))
{
_logger.LogError($"'{MSAspNetCoreToken}' does not match the expected pairing token '{_pairingToken}', request rejected.");
httpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
- return;
+ return Task.CompletedTask;
}
// Handle shutdown from ANCM
@@ -132,14 +132,14 @@ public async Task Invoke(HttpContext httpContext)
// Execute shutdown task on background thread without waiting for completion
var shutdownTask = Task.Run(() => _applicationLifetime.StopApplication());
httpContext.Response.StatusCode = StatusCodes.Status202Accepted;
- return;
+ return Task.CompletedTask;
}
if (Debugger.IsAttached && string.Equals("DEBUG", httpContext.Request.Method, StringComparison.OrdinalIgnoreCase))
{
// The Visual Studio debugger tooling sends a DEBUG request to make IIS & AspNetCoreModule launch the process
// so the debugger can attach. Filter out this request from the app.
- return;
+ return Task.CompletedTask;
}
var bodySizeFeature = httpContext.Features.Get();
@@ -181,7 +181,7 @@ public async Task Invoke(HttpContext httpContext)
httpContext.Features.Set(null);
}
- await _next(httpContext);
+ return _next(httpContext);
}
private static WindowsPrincipal? GetUser(HttpContext context)