title: How can I implement an IConfigurationProvider which loads config values from a database (on-demand)?
body: <p>I'm trying to build a IConfigurationProvider (and IConfigurationSource) for ASP.NET Core 2.1 which will retrieve configuration data from a database.</p>
  
  <p>I've looked a few examples (<a href="https://msdn.microsoft.com/en-us/magazine/mt814420.aspx" rel="nofollow noreferrer">https://msdn.microsoft.com/en-us/magazine/mt814420.aspx</a>, <a href="https://github.com/wintoncode/Winton.Extensions.Configuration.Consul/blob/master/src/Winton.Extensions.Configuration.Consul/ConsulConfigurationProvider.cs" rel="nofollow noreferrer">https://github.com/wintoncode/Winton.Extensions.Configuration.Consul/blob/master/src/Winton.Extensions.Configuration.Consul/ConsulConfigurationProvider.cs</a>) but these seem to load the configuration 'up-front'. I.e. they load all configuration into memory, before any config values are requested.</p>
  
  <p>I'm trying to figure out how to query the configuration database 'on-demand' for example, when Configuraiton.GetValues("xxx") is called.</p>
  
  <p>My Program.cs class looks like this</p>
  
  <pre><code>        var config = new ConfigurationBuilder()
              .SetBasePath(Directory.GetCurrentDirectory())
              .AddDatabase("connectionString")
              .Build();
  
          var key = config.AsEnumerable();
  
          return WebHost
              .CreateDefaultBuilder(args)
              .UseConfiguration(config)
              .UseStartup&lt;Startup&gt;()
              .Build();
      }
  </code></pre>
  
  <p>The issue seems to be with the UseConfiguration extension method, which just enumerates the configuration collection and adds the values at that point in time.</p>
  
  <pre><code>public static IWebHostBuilder UseConfiguration(this IWebHostBuilder hostBuilder, IConfiguration configuration)
  {
    foreach (KeyValuePair&lt;string, string&gt; keyValuePair in configuration.AsEnumerable())
      hostBuilder.UseSetting(keyValuePair.Key, keyValuePair.Value);
    return hostBuilder;
  }
  </code></pre>
  
  <p>Is there any way to defer loading the configuration until values are requested by the Startup.cs class for example?</p>