title: No service for type has been registered
body: <p>I am trying to implement ASP.NET Core middleware, and this is the whole code I have in my project:</p>
  
  <pre><code>public class HostMiddleware : IMiddleware
  {
      public int Count { get; set; }
  
      public async Task InvokeAsync(HttpContext context, RequestDelegate next)
      {
          if (context.Request.Query.ContainsKey("hello"))
          {
              await context.Response.WriteAsync($"Hello World: {++Count}");
          }
          else
          {
              await next.Invoke(context);
          }
      }
  }
  
  public class Startup
  {
      public void ConfigureServices(IServiceCollection services)
      {
      }
  
      public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider provider)
      {
          if (env.IsDevelopment())
          {
              app.UseDeveloperExceptionPage();
          }
  
          app.UseMiddleware&lt;HostMiddleware&gt;();
  
          app.Run(async context =&gt;
          {
              context.Response.StatusCode = 400;
              await context.Response.WriteAsync("Bad request.");
          });
      }
  </code></pre>
  
  <p>However, when I run this server I get the following error:</p>
  
  <blockquote>
    <p>InvalidOperationException: No service for type 'WebApplication4.HostMiddleware' has been registered.</p>
  </blockquote>
  
  <p><a href="https://i.stack.imgur.com/pARdw.png" rel="noreferrer"><img src="https://i.stack.imgur.com/pARdw.png" alt="developer exception page screenshot"></a></p>
  
  <p>Why do I get this error? Why would my middleware need to register any services if I don't use dependency injection in my project? </p>
  
  <p><strong>Update:</strong></p>
  
  <p>For some reason, this error does not occur when I stop using <code>IMiddleware</code>, rename <code>InvokeAsync</code> to <code>Invoke</code> and implement my middleware in the following way:</p>
  
  <pre><code>public class WmsHostMiddleware 
  {
      private readonly RequestDelegate _next;
  
      public int Count { get; set; }
  
      public WmsHostMiddleware(RequestDelegate next)
      {
          _next = next;
      }
  
      public async Task Invoke(HttpContext context)
      {
          if (context.Request.Query.ContainsKey("hello"))
          {
              await context.Response.WriteAsync($"Hello World: {++Count}");
          }
          else
          {
              await _next.Invoke(context);
          }
      }
  }
  </code></pre>
  
  <p>The question is still open - why does this happen? What is the difference? Why do I need to register services when I use <code>IMiddleware</code>.</p>