title: Reload configuration from the controller (asp.net core application)
body: <p>In my .net core mvc (2.2) application I have a configuration, configured from the database. How to reload the configuration from the controller, for example after changing some values in the database? I've tried something like this, but it does not work:</p>
  
  <p><strong>MyConfiguration.cs:</strong></p>
  
  <pre><code>public class MyConfiguration
  {
      public string SmtpServer {get;set;}
  }
  </code></pre>
  
  <p><strong>Startup.cs:</strong></p>
  
  <pre><code>public class Startup
  {
      public IConfiguration Configuration { get; }
  
      public void ConfigureServices(IServiceCollection services)
      {
          services.Configure&lt;MyConfiguration&gt;(options =&gt; {
              options.SmtpServer = Configuration["SmtpServer"];
          });
  
          // ...
  
          services.AddSingleton&lt;IConfiguration&gt;(Configuration); 
      }
  }
  </code></pre>
  
  <p><strong>Program.cs:</strong></p>
  
  <pre><code>public class Program
  {
      public static void Main(string[] args)
      {
          CreateWebHostBuilder(args).Build().Run();
      }
  
      public static IWebHostBuilder CreateWebHostBuilder(string[] args) =&gt;
          WebHost
              .CreateDefaultBuilder(args)  
              .ConfigureAppConfiguration((hostingContext, config) =&gt;
              {
                  config.SetBasePath(Directory.GetCurrentDirectory());
                  config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);                     
                  config.AddEFConfiguration(options =&gt; options.UseNpgsql("connection_string"));
                  config.AddCommandLine(args);
              })
              .UseStartup&lt;Startup&gt;();
  }
  </code></pre>
  
  <p><strong>Controller:</strong></p>
  
  <pre><code>public class TestController : ControllerBase
  {
      private readonly IConfigurationRoot _configurationRoot;
  
      public TestController(IConfiguration configurationRoot)
      { 
          _configurationRoot = (IConfigurationRoot)configurationRoot;    
      } 
  
      [HttpGet] 
      public async Task&lt;IActionResult&gt; Index()
      { 
          // some logic for updating values in the database
  
          // Doesn't work
          _configurationRoot.Reload();
  
          return Ok();
      }
  }
  </code></pre>