Custom App.Config configSections

0

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="&lt;font size=3&gt;--- Se ha registrado un evento {0} ---&lt;p&gt;&lt;/p&gt;&lt;font color=blue&gt;&lt;u&gt;Información detallada&lt;/u&gt;&lt;/font&gt;&lt;p&gt;&lt;/p&gt;&lt;b&gt;{1}&lt;/b&gt;" />
  </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.

    
asked by Archer_A 01.06.2016 в 22:02
source

2 answers

0

The problem is with the location of your ConfigSections element, it must be the first node of your configuration file.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="urlAddresses" type="namespace.UrlRetrieverSection"/>
  </configSections >
  <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="&lt;font size=3&gt;--- Se ha registrado un evento {0} ---&lt;p&gt;&lt;/p&gt;&lt;font color=blue&gt;&lt;u&gt;Información detallada&lt;/u&gt;&lt;/font&gt;&lt;p&gt;&lt;/p&gt;&lt;b&gt;{1}&lt;/b&gt;" />
  </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>
  <!-- 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>

More information here on MSDN.

    
answered by 02.06.2016 / 17:04
source
0

The only strange thing I see is when it comes to registering the type of section in the app.config:

<section name="urlAddresses" type="namespace.UrlRetrieverSection"/>

Is your namespace called "namespace"?

Check in the file of the UrlRetrieverSection class in what namespace it is. In principle it will be by default the name of the application.

For example if you have:

namespace MiAplicacion
{
    public class UrlRetrieverSection: ConfigurationSection

The line in the app.config should be:

<section name="urlAddresses" type="MiAplicacion.UrlRetrieverSection"/>
    
answered by 01.06.2016 в 22:24