Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It gets cached, on first access of a property, so it does not read from the physical file each time you ask for a value. This is why it is necessary to restart an Windows app (or <a href="http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.refreshsection.aspx" rel="noreferrer">Refresh</a> the config) to get the latest value, and why an ASP.Net app automatically restarts when you edit web.config. Why ASP.Net is hard wired to restart is discussed in the references in the answer <a href="https://stackoverflow.com/a/613836/1073107">How to prevent an ASP.NET application restarting when the web.config is modified</a>.</p> <p>We can verify this using <a href="http://ilspy.net/" rel="noreferrer">ILSpy</a> and looking at the internals of System.Configuration:</p> <pre><code>public static NameValueCollection AppSettings { get { object section = ConfigurationManager.GetSection("appSettings"); if (section == null || !(section is NameValueCollection)) { throw new ConfigurationErrorsException(SR.GetString("Config_appsettings_declaration_invalid")); } return (NameValueCollection)section; } } </code></pre> <p>At first, this does indeed look like it will get the section every time. Looking at GetSection:</p> <pre><code>public static object GetSection(string sectionName) { if (string.IsNullOrEmpty(sectionName)) { return null; } ConfigurationManager.PrepareConfigSystem(); return ConfigurationManager.s_configSystem.GetSection(sectionName); } </code></pre> <p>The critical line here is the <code>PrepareConfigSystem()</code> method; this initializes an instance of the <code>IInternalConfigSystem</code> field held by the ConfigurationManager - the concrete type is <code>ClientConfigurationSystem</code> </p> <p>As part of this load, an instance of the <a href="http://msdn.microsoft.com/en-us/library/system.configuration.configuration.aspx" rel="noreferrer">Configuration</a> class is instantiated. This class is effectively an object representation of the config file, and appears to be held by the ClientConfigurationSystem's ClientConfigurationHost property in a static field - hence it is cached.</p> <p>You could test this empirically by doing the following (in a Windows Form or WPF app):</p> <ol> <li>Starting your App up </li> <li>Access a value in app.config </li> <li>Make a change to app.config </li> <li>Check to see whether the new value is present </li> <li>Call <code>ConfigurationManager.RefreshSection("appSettings")</code></li> <li>Check to see if the new value is present.</li> </ol> <p>In fact, I could have saved myself some time if I'd just read the comment on the <a href="http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.refreshsection.aspx" rel="noreferrer">RefreshSection</a> method :-)</p> <pre><code>/// &lt;summary&gt;Refreshes the named section so the next time that it is retrieved it will be re-read from disk.&lt;/summary&gt; /// &lt;param name="sectionName"&gt;The configuration section name or the configuration path and section name of the section to refresh.&lt;/param&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload