title: ASP.NET Core IOC: ASP0000 Calling 'BuildServiceProvider' from application code results in an additional copy of singleton services being created
body: <p>I am working on .Net Core Web App with IdentityServer4 installed and configured on Startup.cs, Here I am loading the token signing certificate from a service that I already registered in the IOC:</p>
  
  <pre><code>        builder.AddSigningCredential(services.BuildServiceProvider().
          GetService&lt;ISecretStoreService&gt;().
          TokenSigningCertificate().Result);
  </code></pre>
  
  <p>Below the full code:</p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
  {
      //....
  
          var builder = services.AddIdentityServer(options =&gt;
              {
                  options.IssuerUri = Configuration.GetValue&lt;string&gt;("IdSrv:IssuerUri"); ;
                  options.PublicOrigin = Configuration.GetValue&lt;string&gt;("IdSrv:PublicOrigin"); ;
                  options.Events.RaiseErrorEvents = true;
                  options.Events.RaiseInformationEvents = true;
                  options.Events.RaiseFailureEvents = true;
                  options.Events.RaiseSuccessEvents = true;
                  options.UserInteraction.LoginUrl = "/Account/Login";
                  options.UserInteraction.LogoutUrl = "/Account/Logout";
                  options.Authentication = new AuthenticationOptions()
                  {
                      CookieLifetime = TimeSpan.FromHours(10),
                      CookieSlidingExpiration = true
                  };
              })
          .AddConfigurationStore(options =&gt;
          {
              options.ConfigureDbContext = b =&gt; b.UseSqlServer(connectionString, sql =&gt; sql.MigrationsAssembly(migrationsAssembly));
          })
          .AddOperationalStore(options =&gt;
          {
              options.ConfigureDbContext = b =&gt; b.UseSqlServer(connectionString, sql =&gt; sql.MigrationsAssembly(migrationsAssembly));
              options.EnableTokenCleanup = true;
          })
          .AddAspNetIdentity&lt;AppUser&gt;()
          .AddExtensionGrantValidator&lt;PhoneNumberTokenGrantValidator&gt;();
  
      if (Environment.IsDevelopment())
      {
          builder.AddDeveloperSigningCredential();
      }
      else
      {
          builder.AddSigningCredential(services.BuildServiceProvider().
              GetService&lt;ISecretStoreService&gt;().
              TokenSigningCertificate().Result);
      }
  
      //....    
  }
  </code></pre>
  
  <p>Also:</p>
  
  <pre><code>    public interface ISecretStoreService
  {
      Task&lt;X509Certificate2&gt; TokenValidationCertificate();
      Task&lt;X509Certificate2&gt; TokenSigningCertificate();
      Task&lt;X509Certificate2&gt; RetrieveCertificate(string certName);
      Task&lt;X509Certificate2&gt; RetrieveSecretAsCertificate(string certName);
  }
  </code></pre>
  
  <p>I am getting this warning and it makes sense as service locator is an anti-pattern:</p>
  
  <blockquote>
    <p>ASP0000    Calling 'BuildServiceProvider' from application code results in an additional copy of singleton services being created. Consider alternatives such as dependency injecting services as parameters to 'Configure'.</p>
  </blockquote>
  
  <p>The question here is, how can I avoid calling <code>services.BuildServiceProvider()</code> inside the Startup class' <code>ConfigureServices(IServiceCollection services){...}</code> to follow the correct pattern and to get rid of this warning?</p>
  
  <p>I went through some answers on SO but it was just to get rid of the warning but still inject the whole Service Locator which is an anti-pattern. I'd say that the only solution in mind is to extend the middleware to add a specific logic (middleware options) so I can call it without the need to construct the <code>services.BuildServiceProvider()</code>.</p>