P
Predrag Stanar
This piece of code works flawlessly in .NET 1.1 but fails in .NET 2.0 beta 2
with following exception:
Unhandled Exception: System.Configuration.ConfigurationErrorsException: An error
occurred creating the configuration section handler for customGroup/customSecti
on: Could not load type 'System.Configuration.NameValueSectionHandler' from asse
mbly 'System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03
f5f7f11d50a3a'. (C:\Temp\TestApp\Test.exe.config line 5) ---> System
..TypeLoadException: Could not load type 'System.Configuration.NameValueSectionHa
ndler' from assembly 'System.Configuration, Version=2.0.0.0, Culture=neutral, Pu
blicKeyToken=b03f5f7f11d50a3a'.
Test.cs
-------
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
namespace TestNs
{
class Test
{
[STAThread]
static void Main(string[] args)
{
NameValueCollection keyValuePairs = ConfigurationSettings.GetConfig(
"customGroup/customSection" ) as NameValueCollection;
if ( null != keyValuePairs )
{
IEnumerator enumKeys = keyValuePairs.GetEnumerator();
while ( enumKeys.MoveNext() )
{
string key = (string) enumKeys.Current;
Console.WriteLine( "Key: {0}, Value: {1}", key,
keyValuePairs.Get( key ) );
}
}
}
}
}
Test.exe.config
---------------
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="customGroup">
<section name="customSection"
type="System.Configuration.NameValueSectionHandler"/>
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v2.0.50215"/>
<supportedRuntime version="v1.1.4322"/>
</startup>
<appSettings>
</appSettings>
<customGroup>
<customSection>
<add key="key1" value="value1"/>
<add key="key2" value="value2"/>
<add key="key3" value="value3"/>
</customSection>
</customGroup>
</configuration>
I tried to add assembly (System) in the customSection's type, but makes no
difference. I do understand that GetConfig() is deprecated, but I have to
maintain compatibility with .NET 1.1 for a while. And beside, even deprecated,
it still should work, shouldn't it?
with following exception:
Unhandled Exception: System.Configuration.ConfigurationErrorsException: An error
occurred creating the configuration section handler for customGroup/customSecti
on: Could not load type 'System.Configuration.NameValueSectionHandler' from asse
mbly 'System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03
f5f7f11d50a3a'. (C:\Temp\TestApp\Test.exe.config line 5) ---> System
..TypeLoadException: Could not load type 'System.Configuration.NameValueSectionHa
ndler' from assembly 'System.Configuration, Version=2.0.0.0, Culture=neutral, Pu
blicKeyToken=b03f5f7f11d50a3a'.
Test.cs
-------
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
namespace TestNs
{
class Test
{
[STAThread]
static void Main(string[] args)
{
NameValueCollection keyValuePairs = ConfigurationSettings.GetConfig(
"customGroup/customSection" ) as NameValueCollection;
if ( null != keyValuePairs )
{
IEnumerator enumKeys = keyValuePairs.GetEnumerator();
while ( enumKeys.MoveNext() )
{
string key = (string) enumKeys.Current;
Console.WriteLine( "Key: {0}, Value: {1}", key,
keyValuePairs.Get( key ) );
}
}
}
}
}
Test.exe.config
---------------
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="customGroup">
<section name="customSection"
type="System.Configuration.NameValueSectionHandler"/>
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v2.0.50215"/>
<supportedRuntime version="v1.1.4322"/>
</startup>
<appSettings>
</appSettings>
<customGroup>
<customSection>
<add key="key1" value="value1"/>
<add key="key2" value="value2"/>
<add key="key3" value="value3"/>
</customSection>
</customGroup>
</configuration>
I tried to add assembly (System) in the customSection's type, but makes no
difference. I do understand that GetConfig() is deprecated, but I have to
maintain compatibility with .NET 1.1 for a while. And beside, even deprecated,
it still should work, shouldn't it?