title: how can i clear the configurationmanager cache for connectionstrings
body: <p>I am trying to change the database name in the connectionstring located in the App.Config of our servicehost at runtime and restart it afterwards connecting to another database. This works fine, but only if the application is shut down for multiple seconds. Shutting down the application for several seconds seems  to clear the cache of the ConfigurationManager.Connectionstrings. The problem is that due to this required shutdown time, I can not use Application.Restart() in my application.</p>
  
  <p>The strange thing about this caching behaviour is that even when the value is updated in memory (in the example the second time it is requested) the updated value is displayed correctly. But when the application is restarted the old value seems to re-emerge.</p>
  
  <p>To verify the behaviour create a new console app.</p>
  
  <p>add an App.Config file</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;configuration&gt;
    &lt;connectionStrings&gt;
      &lt;add name="DomainDBConnectionString" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=InitialDBName;Integrated Security=SSPI;" /&gt;
    &lt;/connectionStrings&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>then add the following code to the Main method</p>
  
  <pre><code>        ConfigurationManager.RefreshSection("connectionStrings");
          DbConnectionStringBuilder builder = new DbConnectionStringBuilder();
          Configuration appConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
          builder.ConnectionString = appConfig.ConnectionStrings.ConnectionStrings["DomainDBConnectionString"].ConnectionString;
  
          //print initial value
          Console.WriteLine("initial " + (string)builder["Initial Catalog"]);
  
          //change value
          builder["Initial Catalog"] = "ChangedDatabaseName";
          appConfig.ConnectionStrings.ConnectionStrings.Remove("DomainDBConnectionString");
          appConfig.ConnectionStrings.ConnectionStrings.Add(new ConnectionStringSettings("DomainDBConnectionString", builder.ConnectionString));
          appConfig.ConnectionStrings.SectionInformation.ForceSave = true;
          appConfig.Save(ConfigurationSaveMode.Full);
          ConfigurationManager.RefreshSection("connectionStrings");
  
          Console.ReadLine();
  
          DbConnectionStringBuilder builder2 = new DbConnectionStringBuilder();
          Configuration appConfig2 = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
          builder2.ConnectionString = appConfig.ConnectionStrings.ConnectionStrings["DomainDBConnectionString"].ConnectionString;
          Console.WriteLine("changed " + (string)builder2["Initial Catalog"]);
  
          Console.ReadLine();
  </code></pre>
  
  <p>To reproduce this behaviour, you need to run the application (by hitting F5) and close it.
  afterwards the solutionname.exe.config file will show up with the modified value.
  If you run the application a second time (<strong>this time by doubleclicking the solutionname.exe</strong>) you will notice the difference in behaviour if you do this immediately after terminating your application, or after you have waited a couple of seconds.</p>
  
  <p>In my opinion the configsection should be re-read because of 
              ConfigurationManager.RefreshSection("connectionStrings");
  but apparenatelly this does not work as advertised.</p>