System.Configuration.AppSettingsSection doc error?

G

Guest

Hi...

Perplexed about something I'm finding here. I just started experimenting
with ConfigurationManager.OpenExeConfiguration() and
ConfigurationManager.OpenMappedExeConfiguration(), and I've found what looks
like a doc bug on MSDN.

http://msdn2.microsoft.com/en-us/li...ation.appsettingssection.settings(VS.80).aspx

The AppSettingSection docs say the Settings property returns a
NameValueCollection; in my code, it says it's returning a
KeyValueConfigurationCollection and that the latter isn't convertible to a
NameValueCollection.

First, I'm wondering if I'm just missing something here (is the msdn page
wrong)?

Second, I'm wondering why System.Configuration.Configuration.AppSettings was
implemented differently than
System.Configuration.ConfigurationManager.AppSettings (which is a
NameValueCollection)?

Thanks
Mark
 
L

Laura T.

Mark said:
Hi...

Perplexed about something I'm finding here. I just started experimenting
with ConfigurationManager.OpenExeConfiguration() and
ConfigurationManager.OpenMappedExeConfiguration(), and I've found what
looks
like a doc bug on MSDN.

http://msdn2.microsoft.com/en-us/li...ation.appsettingssection.settings(VS.80).aspx

The AppSettingSection docs say the Settings property returns a
NameValueCollection; in my code, it says it's returning a
KeyValueConfigurationCollection and that the latter isn't convertible to a
NameValueCollection.

First, I'm wondering if I'm just missing something here (is the msdn page
wrong)?
Yes, a typo. But it's also old page. You'd better off to read the latest,
Framerwork 3.0 pages even when you use 2.0.
The new docs are here.
http://msdn2.microsoft.com/en-us/library/system.configuration.appsettingssection.settings.aspx

Second, I'm wondering why System.Configuration.Configuration.AppSettings
was
implemented differently than
System.Configuration.ConfigurationManager.AppSettings (which is a
NameValueCollection)?
The NameValueCollection is serializable but the
KeyValueConfigurationCollection is not.
I think the reason lies here.
 
G

Guest

Thanks for the tips... I just checked out the 3.0 page and it replaces the
*first* instance of NameValueCollection with KeyValueConfigurationCollection
but not the *second*, so I guess the page still needs fixing.

I know NameValueCollection is a multimap, but it doesn't look like
KeyValueConfigurationCollection is; what will the
Configuration.AppSettings.Settings collection have if you have 2 values with
the same name?

Thanks
Mark
 
J

Jialiang Ge [MSFT]

Hello Mark,

Thank you so much for your great feedbacks on the MSDN articles about
AppSettingsSection.Settings property. Our database has logged the error in
the first article
http://msdn2.microsoft.com/en-us/library/system.configuration.appsettingssec
tion.settings(VS.80).aspx. Instead of using NameValueCollection, it should
have been talking about KeyValueConfigurationCollection. A newer version is
in
http://msdn2.microsoft.com/en-us/library/system.configuration.appsettingssec
tion.settings.aspx. However, as you said, it still seems using an improper
example. I have filed an request to the MSDN team to confirm the issue.
Before I receive their response, please kindly refer to the following
example about AppSettingsSection.Settings property. Sorry for the
inconveniences.

For web application:
Configuration configuration =
WebConfigurationManager.OpenWebConfiguration("~");
AppSettingsSection appSettingsSection =
(AppSettingsSection)configuration.GetSection("appSettings");
// or we can call configuration.AppSettings.Setting
if (appSettingsSection != null) {
foreach (string key in appSettingsSection.Settings.AllKeys) {
Response.Write(key);
}
}
For exe application:
string exePath = System.IO.Path.Combine(Environment.CurrentDirectory,
"ConsoleApplication1.exe");
Configuration config = ConfigurationManager.OpenExeConfiguration(exePath);
AppSettingsSection appSettingsSection =
(AppSettingsSection)config.GetSection("appSettings");
// or we can call configuration.AppSettings.Setting
foreach (string key in appSettingsSection.Settings.AllKeys)
{
Console.Write(key);
}

For your question about the difference between NameValueCollection and
KeyValueConfigurationCollection: both implement the interface Icollection
and Ienumerable, but NameValueCollection also implements ISerializable and
IdeserializationCallback. Besides, KeyValueConfigurationCollection does not
support a "Set" method. We could use NameValueCollection.Set to reset a
key's value.

In the configuration file, if a key has multiple values, the default
behavior of ConfigurationManager.AppSettings and
Configuration.AppSettings.Settings is to overwrite the previous value with
the new. That is to say, if in <appSettings> tag, there are items: <add
key="TestKey" value="1"/> <add key="TestKey" value="2"/>,
Configuration.AppSettings.Settings or ConfigurationManager.AppSettings only
returns "2" as "TestKey" by design. Internally, it is using
collection[key] = value; instead of collection.Add(key, value);, therefore,
the old value will be overwritten by the new one, even if
NameValueCollection has methods like GetValues. The development team has
confirmed that it is by design because in majority of the cases, a user
wants to overwrite the value (such as in lower level web.config file for
asp.net applications) instead of adding one more value under the same key.
I am sorry if this design decision does not fit your situation. In order to
support multiple values for a same key, we need to customize the
ConfigurationSectionHandler. Please refer to the following article for more
detailed information.
http://www.codeproject.com/dotnet/namevaluemultiple.asp

Sincerely,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
For MSDN subscribers whose posts are left unanswered, please check this
document: http://blogs.msdn.com/msdnts/pages/postingAlias.aspx

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express/Windows Mail, please make sure
you clear the check box "Tools/Options/Read: Get 300 headers at a time" to
see your reply promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jialiang Ge [MSFT]

Hello Mark,

Sorry for my delayed response. I finally get the confirmation of the issue
from the internal MSDN library team. We have fixed the improper examples
and typos, and they will be available in the next release of MSDN library.
Thank you very much for your noticing. I believe that other community
members will also benefit from your experience sharing. Without the input
of numerous intelligent and warmhearted customers like you, our MSDN
Managed Newsgroup can never be as comprehensive and helpful as it is today.
I appreciate your time and efforts working with us.

I am contacting you to see whether there is anything else we can do for
you. If you have any comments on our support service, website design, our
product limitation, our process, and etc., please don't hesitate to let me
know. I will do my best to follow up.

Thank you for using our MSDN Managed Newsgroup Support Service.

Best regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from your issue.
=================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top