Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Difference between ConfigurationManager.GetSection and Configuration.GetSection?

I'm trying to create a custom config file section based on AppSettings:

<configSections>
  <section name="customConfiguration" 
           type="System.Configuration.AppSettingsSection, 
                 System.Configuration, 
                 Version=2.0.0.0, Culture=neutral, 
                 PublicKeyToken=b03f5f7f11d50a3a"/>
</configSections>

When I tried reading it via ConfigurationManager.GetSection("customConfiguration") the object returned was of type System.Configuration.KeyValueInternalCollection. I was unable to read the values of this collection, although I could see the keys, and I couldn't cast it to an AppSettingsSection.

This Stackoverflow answer suggests I should use

Configuration config = 
    ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AppSettingsSection customSettingSection = 
    (AppSettingsSection)config.GetSection("customConfiguration");

This worked. My question is: What is the difference between ConfigurationManager.GetSection() and Configuration.GetSection()? When should I use one and when should I use the other?

like image 312
Simon Tewsi Avatar asked Jul 21 '13 23:07

Simon Tewsi


People also ask

What is configuration GetSection()?

Configuration settings are contained within sections that group similar settings together for convenience. The GetSection method retrieves a configuration section by its name.

What is the use of ConfigurationManager appSettings?

The appSettings element is primarily for custom app settings that are unique to your app, and have no . NET (or ASP.NET) equivalents, like a list of your clients' email addresses, or the tagline and copyright info for your cat blog.

Is ConfigurationManager thread safe?

ConfigurationManager. GetSection(string) is a public static member, and since msdn states 'Any public static (Shared in Visual Basic) members of this type are thread safe', you can assume it's safe to use.


1 Answers

According to the MSDN documentation on the Configuration Class http://msdn.microsoft.com/en-us/library/system.configuration.configuration.aspx,

If your application needs read-only access to its own configuration, it is recommended that you use the GetSection method overloads for Web applications. For client application, use the GetSection method.

These methods provide access to the cached configuration values for the current application, which has better performance than the Configuration class.

Specifically, in client applications, the ConfigurationManager retrieves a configuration file obtained by merging the application configuration file, the local user configuration file, and the roaming configuration file.

like image 111
Claies Avatar answered Sep 19 '22 12:09

Claies