Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the appsettings file attribute override what is in the app.config?

Tags:

The appsettings tag in the app.config has a file attribute:

<appSettings file="other.config"> .. .. </appSettings> 

How does this work? Will it merge what is in the appSettings (original) with the other.config file? Or will it overwrite it? What if the other.config file doesn't exist, should it crash?

I'm trying it myself and if a key isn't in the original, it doesn't seem to read it from the other.config?

Should the other.config file have just xml nodes, or should it all be inside a appsettings element?

<appSettings>   <userId>123</userId> </appSettings> 

or

<userId>123</userId> 
like image 890
codecompleting Avatar asked Jan 24 '12 16:01

codecompleting


People also ask

What is appSettings in app config?

The <appSettings> element stores custom application configuration information, such as database connection strings, file paths, XML Web service URLs, or any other custom configuration information for an application.

Why appSettings keys in web config are not case sensitive?

Why appsettings keys (in web. config) are not case sensitive ? The default comparer is a CaseInsensitiveComparer that uses the conventions of the invariant culture; that is, key comparisons are case-insensitive by default.

How does app config work?

App. Config is an XML file that is used as a configuration file for your application. In other words, you store inside it any setting that you may want to change without having to change code (and recompiling). It is often used to store connection strings.


2 Answers

  • If the file doesn't exist it will not crash, it will just be ignored.
  • The external config has to contain the <appSettings> node so your first example is correct.
  • The value in the external file will take priority, if no value is present then the app.config value is used.

Does that cover off everything?

like image 175
DoctorMick Avatar answered May 17 '23 02:05

DoctorMick


One of the best answers on the subject is here: ASP.NET web.config: configSource vs. file attributes - Credit to @Massimiliano Peluso

file attribute

  • Specifies a relative path to an external file that contains custom application configuration settings
  • specific to the appSettings section
  • will merge (and override) settings in the .config file
  • will not cause web application to restart when modifying the specified file
  • http://msdn.microsoft.com/en-US/library/ms228154(v=vs.100).aspx
  • Using the Configuration.AppSettings.Settings.Add API will result in all settings being merged back into the main .config on a Configuration.Save call.
  • since .NET 1.1
  • Exception is not thrown if file does not exist.

configSource attribute

  • can apply to most sections of a configuration file, not just appSettings
  • will override the entire section with the external file, no merging
  • CAN cause web application to restart
  • http://msdn.microsoft.com/en-US/library/system.configuration.sectioninformation.configsource(v=vs.100).aspx
  • Using the Configuration.AppSettings.Settings.Add API will result in all settings being added to the file specified in configSource on a Configuration.Save call.
  • since .NET 2.0
  • System.Configuration.ConfigurationErrorsException is thrown if config source file does not exist.

The file attribute specifies an external file containing custom settings like you do in the appSettings entry of the web.config file. Meanwhile, the external file specified in the configSource attribute contains the settings for the section which you declare the configSource for. For example, if you use the configSource attribute of the pages section, then the external file will contain the settings for the pages section.

The custom settings declared in the external config specifified in the file attribute will be merged with the settings in the appSettings section in the web.config file. In the meanwhile, the configSource does not support merging, it means that you'll have to move the entire section settings into the external file.

http://www.codeproject.com/Messages/1463547/Re-difference-between-configSource-and-file-attrib.aspx

like image 38
JJS Avatar answered May 17 '23 04:05

JJS