Reload configuration from the controller (asp.net core application)
Asked Answered
G

1

9

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:

MyConfiguration.cs:

public class MyConfiguration
{
    public string SmtpServer {get;set;}
}

Startup.cs:

public class Startup
{
    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<MyConfiguration>(options => {
            options.SmtpServer = Configuration["SmtpServer"];
        });

        // ...

        services.AddSingleton<IConfiguration>(Configuration); 
    }
}

Program.cs:

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost
            .CreateDefaultBuilder(args)  
            .ConfigureAppConfiguration((hostingContext, config) =>
            {
                config.SetBasePath(Directory.GetCurrentDirectory());
                config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);                     
                config.AddEFConfiguration(options => options.UseNpgsql("connection_string"));
                config.AddCommandLine(args);
            })
            .UseStartup<Startup>();
}

Controller:

public class TestController : ControllerBase
{
    private readonly IConfigurationRoot _configurationRoot;

    public TestController(IConfiguration configurationRoot)
    { 
        _configurationRoot = (IConfigurationRoot)configurationRoot;    
    } 

    [HttpGet] 
    public async Task<IActionResult> Index()
    { 
        // some logic for updating values in the database

        // Doesn't work
        _configurationRoot.Reload();

        return Ok();
    }
}
Gastrovascular answered 5/11, 2019 at 15:11 Comment(3)
Read up on IOptionsMonitor learn.microsoft.com/en-us/aspnet/core/fundamentals/…Celik
In general, configuration is set at app startup. Certain config providers support a concept of reloading config, and in combination with using IOptionsSnapshot, you can get updated values per request. However, the config provider has to support reload. A database provider typically won't.Lilylivered
Normally if you configuration is coming from a database you should normally create some repository which you can inject and use whenever you need it.Denton
C
3

Relaod/Refreh Configuration.

private IConfigurationRoot configRoot { get; set; }
private IOptionsSnapshot<**your config class**> IConfigurationAdapter;
       
     public XYZController(IOptionsSnapshot<**your config class**> configurationAdapter,IConfiguration con)
        {
            if (configurationAdapter != null)
                this.IConfigurationAdapter = configurationAdapter;
            this.configRoot = con as IConfigurationRoot;
        }

        [HttpGet("/Reload")]
        public IActionResult Reload()
        {
            if (configRoot != null)
            {
                configRoot.Reload();
                return Ok("Application ConfigurationSetting Refreshed...!");
            }
            else
            {
                return Ok("Application ConfigurationSetting Not Refreshed...!");
            }
        }
Canadianism answered 29/9, 2021 at 19:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.