Configuration Section

S

shapper

Hello,

I am trying to get a custom configuration section as follows:

EnvironmentConfigurationSection environment =
System.Configuration.ConfigurationManager.GetSection("environment") as
EnvironmentConfigurationSection;

But I keep getting the same error:

The Configuration property 'Cdn' may not be derived from
ConfigurationSection

Can someone tell me what am I doing wrong? My code is the following:

public partial class EnvironmentConfigurationSection :
ConfigurationSection {

[ConfigurationProperty("Cdn")]
public CdnConfigurationSection Cdn { get { return this["Cdn"] as
CdnConfigurationSection; } }

} // EnvironmentConfigurationSection

public class CdnConfigurationSection : ConfigurationSection {

[ConfigurationProperty("google", IsRequired = true, IsKey = false,
IsDefaultCollection = true)]
public CdnGoogleConfigurationElementCollection Google { get { return
this["google"] as CdnGoogleConfigurationElementCollection; } }

} // CdnConfigurationSection

public class CdnGoogleConfigurationElementCollection :
ConfigurationElementCollection {

[ConfigurationProperty("key", IsRequired = true)]
public String Key { get { return this["key"] as String; } } // Key

public CdnGoogleConfigurationElement this[Int32 index] {
get {
return base.BaseGet(index) as CdnGoogleConfigurationElement;
}
set {
if (base.BaseGet(index) != null) {
base.BaseRemoveAt(index);
}
this.BaseAdd(index, value);
}
} // CdnGoogleConfigurationElement

protected override ConfigurationElement CreateNewElement() {
return new CdnGoogleConfigurationElement();
} // CreateNewElement

protected override Object GetElementKey(ConfigurationElement
element) {
return ((CdnGoogleConfigurationElement)element).Name;
} // GetElementKey

} // CdnGoogleConfigurationElementCollection

public class CdnGoogleConfigurationElement : ConfigurationElement {

[ConfigurationProperty("name", IsRequired = true)]
public String Name { get { return this["name"] as String; } }

[ConfigurationProperty("version", IsRequired = true)]
public String Version { get { return this["version"] as String; }}

} // CdnGoogleConfigurationElement


Thank You,

Miguel
 
J

Jeff Johnson

I am trying to get a custom configuration section as follows:

EnvironmentConfigurationSection environment =
System.Configuration.ConfigurationManager.GetSection("environment") as
EnvironmentConfigurationSection;

But I keep getting the same error:

The Configuration property 'Cdn' may not be derived from
ConfigurationSection

Can someone tell me what am I doing wrong? My code is the following:

It sounds like you're not allowed to have configuration sections within
configuration sections.
 
F

Felix Palmen

* Jeff Johnson said:
It sounds like you're not allowed to have configuration sections within
configuration sections.

Exactly, and this can be a little confusing at first. In fact, a
ConfigurationSection is always "toplevel". To nest further,
ConfigurationElement is what you want to use. While
ConfigurationProperty translates to an XML attribute,
ConfigurationElement translates to a nested tag.

Regards,
Felix
 
S

shapper

It sounds like you're not allowed to have configuration sections within
configuration sections.

Exactly, and this can be a little confusing at first. In fact, a
ConfigurationSection is always "toplevel". To nest further,
ConfigurationElement is what you want to use. While
ConfigurationProperty translates to an XML attribute,
ConfigurationElement translates to a nested tag.

Regards,
Felix

--
 Felix Palmen       (Zirias)  + [PGP] Felix Palmen <[email protected]>
 web:  http://palmen-it.de/ |            http://palmen-it.de/pub.txt
 my open source projects:     |   Fingerprint: ED9B 62D0 BE39 32F9 2488
 http://palmen-it.de/?pg=pro +                5D0C 8177 9D80 5ECF F683

Using Configuration Element worked fine.

Thank you,
Miguel
 

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