Benefits of .Net's AppSettingsReader vs ConfigurationManager for reading application configuration settings
Asked Answered
P

2

9

Is there a substantial difference between the AppSettingsReader class and the AppSettings member of the ConfigurationManager class in .Net 3.5?

I'm building out some legacy code and a previous developer used AppSettingsReader.GetValue(), whereas I am more prone to use ConfigurationManager.AppSettings.Get().

Looking at the internals, AppSettingReader seems to be more typesafe, but its usage seems slightly more verbose. If the app settings I'm retrieving are well-known and fairly static, is there any benefit to using AppSettingsReader?

Potomac answered 16/8, 2010 at 22:3 Comment(0)
F
7

Not really. Internally AppSettingsReader just used the ConfigurationManager.AppSettings.

There is basically just a bit of fluff around checking that it exists, checking that what is going on is ok etc.

One thing is though that it will throw an exception when there is no element in the AppSettings..

    // From CTOR
    this.map = ConfigurationManager.AppSettings;

    ....
    public object GetValue(string key, Type type)

       ...

       string item = this.map[key];
       if (item == null)
       {
           throw new InvalidOperationException(SR.GetString("AppSettingsReaderNoKey", new object[] { key }));
       }

I think that most people will just use ConfigurationManager but the real answer is 'wahtever floats your boat'.

Fabrizio answered 17/11, 2011 at 10:53 Comment(0)
F
0

In addition to Dave Walker's great answer, let me point out that the AppSettingsReader caches the ConfigurationManager.AppSettings in its constructor.

So, in theory, the AppSettingsReader may have slightly better performance, especially if you're reading a lot of settings from the same instance, and if you have some logic to transform a setting into its final type similar to what the AppSettingsReader does.

Of course, you can always cache the ConfigurationManager.AppSettings yourself to get around this.

Fundus answered 22/3, 2023 at 4:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.