2

I have an ASP.NET Core application that has a connection string key in the config file.

If its value is not correct (for example database name, ip address, etc), i wish to be able to re-register this dbcontext service with the new connection string after the user has changed it manually in the config file.

How to achieve this? Is this considered a bad practice?

Thank you.

2
  • 1
    Changes to the web.config should trigger the app to be reloaded by IIS once there is zero connection left to the app. Commented Jan 10, 2019 at 8:13
  • Its in the appsettings.json not web.config. i have closed all tabs but it didnt seem to work.. (maybe because of kept sessions?) Commented Jan 10, 2019 at 8:19

2 Answers 2

4

I see that you are using asp.net core.

Ideally you should not change your connection string when the application is running because it may cause a RESET of your web application (depending on the startup configurations and web server).

For answering your question, you can change configuration when application is running. ReloadOnChange parameter would be useful for you.

Below code sets reloadOnChange to true whenever there is change in appsettings.json configuration file.

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

Reference: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-2.2

Sign up to request clarification or add additional context in comments.

3 Comments

Why is this not working on debug mode? i dont have the appsettings.json file if i run on debug, but even if i do 'Copy to output directory' and it is infact copied, the change to the appsettings has no effect
are you injecting IOptionSnapshot or something ? This reload on change should work as per expectation mentioned in question.
Its working well when i publish the site, but not when i debug it. And no, im not injecting IOptionSnapshot
0

With .NET Core, you need to configure your Startup OWIN app correctly for it to automatically reload your settings file.

In your startup class where you register the appsettings.json file, you need to tell it that it should reload on change. You pass it as a parameter. e.g

    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json", reloadOnChange: true)
        Configuration = builder.Build();
    }

Try something along the lines of the above code

To Elaborate on the above.

Changing setting files like this should ideally be managed through a deployment tool such as Octopus. This way you can define variables on the tool that will be used to replace app settings upon deployment. :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.