title: Why is it recommended for a middleware to be async in ASP.NET Core?
body: <p>Why is it recommended for a middleware to be async in ASP.NET Core?</p>
  
  <p>E.g. in <a href="https://www.tutorialsteacher.com/core/how-to-add-custom-middleware-aspnet-core" rel="nofollow noreferrer">this</a> tutorial it is recommended to make the middleware custom and I can not understand the reason behind it.</p>
  
  <pre><code>public class MyMiddleware
  {
      private readonly RequestDelegate _next;
      private readonly ILogger _logger;
  
      public MyMiddleware(RequestDelegate next, ILoggerFactory logFactory)
      {
          _next = next;
  
          _logger = logFactory.CreateLogger("MyMiddleware");
      }
  
      public async Task Invoke(HttpContext httpContext)
      {
          _logger.LogInformation("MyMiddleware executing..");
  
          await _next(httpContext); // calling next middleware
  
      }
  }
  
  // Extension method used to add the middleware to the HTTP request pipeline.
  public static class MyMiddlewareExtensions
  {
      public static IApplicationBuilder UseMyMiddleware(this IApplicationBuilder builder)
      {
          return builder.UseMiddleware&lt;MyMiddleware&gt;();
      }
  } 
  </code></pre>