title: Adding custom middleware not working when using IMiddleware
body: <p>I am trying to add a custom middleware to the pipeline (to be easier I will pick the .NET Core documentation example).
  Let's say we want to have the Spanish culture set whenever a call to API is fired.
  Here's the code which runs perfectly:</p>
  
  <pre><code>public class RequestCultureMiddleware
  {
      private readonly RequestDelegate _next;
  
      public RequestCultureMiddleware(RequestDelegate next)
      {
          _next = next;
      }
  
      public async Task InvokeAsync(HttpContext context)
      {
          CultureInfo.CurrentCulture = new CultureInfo("es-ES");
          CultureInfo.CurrentUICulture = new CultureInfo("es-ES");
  
          // Call the next delegate/middleware in the pipeline
          await _next(context);
      }
  }
  
  public static class RequestCultureMiddlewareExtensions
  {
      public static IApplicationBuilder UseRequestCulture(
          this IApplicationBuilder builder)
      {
          return builder.UseMiddleware&lt;RequestCultureMiddleware&gt;();
      }
  }
  </code></pre>
  
  <p>and the Startup class:</p>
  
  <pre><code>public class Startup
  {
      public Startup(IConfiguration configuration)
      {
          Configuration = configuration;
      }
  
      public IConfiguration Configuration { get; }
  
      // This method gets called by the runtime. Use this method to add services to the container.
      public void ConfigureServices(IServiceCollection services)
      {
          services.AddControllers();
      }
  
      // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
      public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
      {
          if (env.IsDevelopment())
          {
              app.UseDeveloperExceptionPage();
          }
  
          //here is our custom middleware!
          app.UseRequestCulture();
  
          app.UseHttpsRedirection();
  
          app.UseRouting();
  
          app.UseAuthorization();
  
          app.UseEndpoints(endpoints =&gt;
          {
              endpoints.MapControllers();
          });
      }
  }
  </code></pre>
  
  <p>That's fine, but as you can see, the RequestCultureMiddleware does not implement an interface or a base class/abstract class. You just need to remember when defining a middleware to create a constructor that receives the next middleware and also you need to create a method called specifically "InvokeAsync" with "HttpContext" as a parameter.</p>
  
  <p>I tried to find a contract... a base class or an interface and guess what, we have "IMiddleware" which is part of "Microsoft.AspNetCore.Http" assembly. Wow, that's perfect. Let's implement it.</p>
  
  <p>The interface looks like this:</p>
  
  <pre><code>namespace Microsoft.AspNetCore.Http
  {
      //
      // Summary:
      //     Defines middleware that can be added to the application's request pipeline.
      public interface IMiddleware
      {
          //
          // Summary:
          //     Request handling method.
          //
          // Parameters:
          //   context:
          //     The Microsoft.AspNetCore.Http.HttpContext for the current request.
          //
          //   next:
          //     The delegate representing the remaining middleware in the request pipeline.
          //
          // Returns:
          //     A System.Threading.Tasks.Task that represents the execution of this middleware.
          Task InvokeAsync(HttpContext context, RequestDelegate next);
      }
  }
  </code></pre>
  
  <p>And here is the implementation:</p>
  
  <pre><code>    public class RequestCultureMiddleware : IMiddleware
      {
  
          public Task InvokeAsync(HttpContext context, RequestDelegate next)
          {
              CultureInfo.CurrentCulture = new CultureInfo("es-ES");
              CultureInfo.CurrentUICulture = new CultureInfo("es-ES");
  
              // Call the next delegate/middleware in the pipeline
              return next(context);
          }
      }
  
  
      public static class RequestCultureMiddlewareExtensions
      {
          public static IApplicationBuilder UseRequestCulture(
              this IApplicationBuilder builder)
          {
              return builder.UseMiddleware&lt;RequestCultureMiddleware&gt;();
          }
      }
  }
  </code></pre>
  
  <p>But, when running the API I am getting the following error at run-time:</p>
  
  <pre><code>System.InvalidOperationException: No service for type 'WebApplication1.RequestCultureMiddleware' has been registered.
     at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
     at Microsoft.AspNetCore.Http.MiddlewareFactory.Create(Type middlewareType)
     at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.&lt;&gt;c__DisplayClass5_1.&lt;&lt;UseMiddlewareInterface&gt;b__1&gt;d.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
  </code></pre>
  
  <p>How exactly I am supposed to register this middleware if not by using the extension "UseMiddleware"?
  Thanks.</p>