title: Register Service in ASP.NET Core as part of a middleware registration
body: <p>Using ASP.NET Core 3.1 I am adding a <code>SitemapMiddleware</code> on Startup's Configure method:</p>
  <pre><code>public void Configure(IApplicationBuilder builder, IWebHostEnvironment environment) {
    builder.UseSitemap();
  }
  </code></pre>
  <p>Where the <code>UseSitemap</code> extension is:</p>
  <pre><code>public static class SitemapMiddlewareExtensions {
    public static IApplicationBuilder UseSitemap(this IApplicationBuilder builder) {
      return builder.MapWhen(x =&gt; x.Request.Path.StartsWithSegments(&quot;/sitemap.xml&quot;), 
        x =&gt; x.UseMiddleware&lt;SitemapMiddleware&gt;(route));
    } 
  }
  </code></pre>
  <p>But the middleware uses an <code>ISitemapService</code> which I need to register as:</p>
  <pre><code>services.AddScoped&lt;ISitemapService, SitemapService&gt;();
  </code></pre>
  <p>How can I do this inside the method <code>UseSitemap</code>?</p>
  <p>Or maybe create an IServicesCollection extension to use in Startup's ConfigureServices that registers the SitemapService?</p>