0

My requirement is, on the first time loading the application i want to fetch information about connection string from a database and next time onward's connect using the new connection string (which is fetched from the db) is this possible in .net.? The connection string is written in web.config file
do I need to restart IIS whenever the web.config changes?

I tried this:

public ActionResult Index()
{
    UpdateSetting("test", "123");
    UpdateConnectionString("testcon", "12345");
    return View();
}

/// <summary>
/// Updates the setting.
/// </summary>
/// <param name="key">The key.</param>
/// <param name="value">The value.</param>
public void UpdateSetting(string key, string value)
{
    Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
    if (config.AppSettings.Settings[key] == null)
    {
        config.AppSettings.Settings.Add(key, value);
    }
    else
    {
        config.AppSettings.Settings[key].Value = value;
    }
    config.Save();
    ConfigurationManager.RefreshSection("appSettings");
}

/// <summary>
/// Updates the connection string.
/// </summary>
/// <param name="key">The key.</param>
/// <param name="value">The value.</param>
public void UpdateConnectionString(string key, string value)
{
    Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
    if (config.ConnectionStrings.ConnectionStrings[key] == null)
    {
        config.ConnectionStrings.ConnectionStrings.Add(new ConnectionStringSettings(key, value));
    }
    else
    {
        config.ConnectionStrings.ConnectionStrings[key].ConnectionString = value;
    }
    config.Save();
    ConfigurationManager.RefreshSection("connectionStrings");
}

This function will add a new connection string but is there any other method so that i don't want to restart the application?

5
  • 3
    If you change the web.config, the app will restart - there's no way to stop that. However, just store the connection string somewhere else. Commented Jan 4, 2018 at 14:05
  • If you're retrieving the connection string dynamically because you expect it to change, then there's no reason to store it in web.config. Just retrieve it on startup and cache it. Maybe refresh it once in a while. If you don't expect it to change then there's no reason for the whole process. Just get it from wherever it comes from and put it in web.config the normal way. Commented Jan 4, 2018 at 14:29
  • @DavidG sir, where can i store connection string other than the config file Commented Jan 5, 2018 at 4:13
  • @ScottHannen i am trying to connect to application from another application (using web service) and when ever the request comes from cross domain i have to check whether the connection is a authentic one ,if so i have to make the application behavior Specific for that user (means should use db for that user) there can be 1000s of such users how could achieve this ? i understood that change is web config wont work Commented Jan 5, 2018 at 4:20
  • It's a little bit hard to follow. An application that calls a web service shouldn't even know whether the web service uses a database, or what sort it is, that there are more than one, or what the connection strings are. All of those details should be hidden from the consumer of the web service. Commented Jan 5, 2018 at 13:45

2 Answers 2

0

The application setting is static and should not be modified in the runtime. I don't know why you have to store the connection in the config file, it doesn't make sense. Try another solution, update the setting by a script before start or store the setting in another or just retrieve every time.

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

1 Comment

Thankyou @Frankx for answering ! where i can store the connection string other than config file?? Actually when ever the application start for the first time the connection string point to a Db , from that db it decides to which db the application should be connected , Is this possible ?
0

There are multiple options to achieve this.

  1. If you're using DI, you can register the connection string factory as a singleton, and then just try to get connection string wherever you'd like. It'll fetch it only once for the application.

  2. Update your UpdateConnectionString method to cache the retrieved value in some static variable (not a great way to do it, though) and then, and check the cache before retrieving the value. If cache is not null, use its value.

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.