title: How to correctly resolve services to use in the ConfigureServices() in ASP.NET Core 3.1?
body: <p>I have an ASP.NET Core 3.1 based app. During the app startup, in the <code>ConfigureServices(IServiceCollection services)</code> I want to register my services. But during configuring services, I want to boot the app based on settings found in the database.</p>
  
  <p>Here is my code 
  </p>
  
  <pre class="lang-cs prettyprint-override"><code>public void ConfigureServices(IServiceCollection services)
  {
      // Register context
      services.AddDbContext&lt;AppDbContext&gt;(options =&gt;
      {
          options.UseMySql(Configuration.GetConnectionString("MySqlServerConnection"));
      });
  
      // Reister the setting provider
      services.AddSingleton&lt;IAppSetting, AppSettings&gt;();
  
      // Create a resolver which is WRONG!!
      var resolver = services.BuildServiceProvider();
  
      var setting = resolver.GetService&lt;IAppSetting&gt;();
  
      List&lt;string&gt; allowedCors = new List&lt;string&gt;()
      {
           setting.MainUrl.ToString()
      };
  
      if (setting.HasCustomAssetsUri)
      {
          allowedCors.Add(settings.AssetsUrl.ToString());
      }
  
      if (settings.HasCustomPhotosUri)
      {
          allowedCors.Add(settings.PhotosUrl.ToString());
      }
  
      services.AddCors(options =&gt;
      {
          options.AddPolicy("AllowSubDomainTraffic",
          builder =&gt;
          {
              builder.WithOrigins(allowedCors.ToArray())
                     .AllowAnyHeader()
                     .AllowAnyMethod();
          });
      });
  
      // More services
  }
  </code></pre>
  
  <p>As you can see in the above code, I register the <code>IAppSetting</code> but I immediately want to use to access the database so get the current configuration. I am currently calling <code>services.BuildServiceProvider()</code> to create a new resolver in which I can then use it to create an instance of the <code>IAppSetting</code>.</p>
  
  <p>My <code>AppSetting</code> implementation depends on the <code>AppDbContext</code> which is also registered. Here is my AppSetting</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class AppSetting : IAppSetting
  {
      private AppDbContext context;
  
      public AppSetting(AppDbContext context)
      {
          this.context = context;
      }
  
      // methods...
  }
  </code></pre>
  
  <p>Calling <code>BuildServiceProvider()</code> will result in an additional copy of singleton services being created. so I am trying to avoid having to do this.</p>
  
  <p>How can I correctly register and then resolve <code>IAppSetting</code> in the <code>ConfigureServices()</code> method so I can use it to access the settings found in the database?</p>
  
  <p><strong>Updated</strong></p>
  
  <p>I attempted to use the solution presented by Nkosi, but getting the following error</p>
  
  <blockquote>
    <p>System.InvalidOperationException: 'Cannot resolve scoped service
    'IAppSetting' from root provider.'</p>
  </blockquote>
  
  <p>Here is the full stack trace.</p>
  
  <pre class="lang-cs prettyprint-override"><code>This exception was originally thrown at this call stack:
      Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.ValidateResolution(System.Type, Microsoft.Extensions.DependencyInjection.IServiceScope, Microsoft.Extensions.DependencyInjection.IServiceScope)
      Microsoft.Extensions.DependencyInjection.ServiceProvider.Microsoft.Extensions.DependencyInjection.ServiceLookup.IServiceProviderEngineCallback.OnResolve(System.Type, Microsoft.Extensions.DependencyInjection.IServiceScope)
      Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(System.Type, Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope)
      Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(System.Type)
      Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(System.IServiceProvider, System.Type)
      Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(Microsoft.Extensions.DependencyInjection.ServiceLookup.FactoryCallSite, Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext)
      Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitDisposeCache(Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceCallSite, Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext)
      ...
      [Call Stack Truncated]
  </code></pre>