I'm trying to add customs values, outside of appSettings, because I need to map these values and take them as part of an object ...
In this same forum but in English, I read how to do it and try to replicate it without success .. this is my App.Config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<!-- Configuracion de Email -->
<add key="UserEmail" value="[email protected]" />
<add key="PasswordEmail" value="123456" />
<!-- Configuracion de estructura de Email -->
<add key="SubjetEmail" value="Evento [{0}]" />
<add key="BodyEmail" value="<font size=3>--- Se ha registrado un evento {0} ---<p></p><font color=blue><u>Información detallada</u></font><p></p><b>{1}</b>" />
</appSettings>
<!-- Cadenas de Conexion a DB -->
<connectionStrings>
<add name="mibasededatos" connectionString="Data Source=x.x.x.x;Initial Catalog=mibasededatos;user id=miusuario;password=mipassword" providerName="System.Data.SqlClient" />
</connectionStrings>
<configSections>
<section name="urlAddresses" type="namespace.UrlRetrieverSection"/>
</configSections >
<!-- URL (Agregar o quitar) -->
<urlAddresses>
<add name="Google" url="http://www.google.com" />
</urlAddresses>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
And the UrlRetrieverSection class
public class UrlRetrieverSection : ConfigurationSection
{
[ConfigurationProperty("", IsDefaultCollection = true, IsRequired = true)]
public UrlCollection UrlAddresses
{
get
{
return (UrlCollection)this[""];
}
set
{
this[""] = value;
}
}
}
public class UrlCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new UrlElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((UrlElement)element).Name;
}
}
public class UrlElement : ConfigurationElement
{
[ConfigurationProperty("name", IsRequired = true, IsKey = true)]
public string Name
{
get
{
return (string)this["name"];
}
set
{
this["name"] = value;
}
}
[ConfigurationProperty("url", IsRequired = true)]
public string Url
{
get
{
return (string)this["url"];
}
set
{
this["url"] = value;
}
}
}
Then read it as follows
UrlRetrieverSection UrlAddresses = (UrlRetrieverSection)ConfigurationManager.GetSection("urlAddresses");
And here comes the problem, apparently the structure of the App.Config is corrupted, because the error the error is as follows.
Configuration system initialization failed
================================================================================================= ===============
In the end I was able to correct it. The problem is that
<configSections>
<section name="urlAddresses" type="namespace.UrlRetrieverSection"/>
</configSections >
It must be declared at the start of the App.Config, just after configuration and before appSettings.
Greetings.