Nolonar
Nolonar

Reputation: 6132

What's wrong with my App.config?

I've been tasked to extend our app's App.config by creating a <configSection> in which we'd define a relatively complex object in Xml. The config looks like this:

<configuration>
  <appSettings>
    <add key="MinWaitTime" value="150" />
    <add key="MaxWaitTime" value="900" />
  </appSettings>
  <configSections>
    <section name="clientconfig" type="KeepWarm.ClientConfig" />
  </configSections>
  <clientconfig username="test user" password="test password">
    <urls>
      <add url="test1" />
      <add url="test2" />
      <add url="test3" />
    </urls>
  </clientconfig>
</configuration>

Eventually, I'll have to be able to define multiple <clientconfig>, but I'm already struggling getting only one to work.

namespace KeepWarm
{
    public class ClientConfig : ConfigurationSection
    {
        [ConfigurationProperty("username", IsRequired = true)]
        public string UserName
        {
            get { return (string)base["username"]; }
            set { base["username"] = value; }
        }

        [ConfigurationProperty("password", IsRequired = true)]
        public string Password
        {
            get { return (string)base["password"]; }
            set { base["password"] = value; }
        }

        [ConfigurationProperty("urls", IsRequired = true)]
        public UrlCollection Urls
        {
            get { return (UrlCollection)base["urls"]; }
            set { base["urls"] = value; }
        }
    }

    [ConfigurationCollection(typeof(UrlElement))]
    public class UrlCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new UrlElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((UrlElement)element).Url;
        }
    }

    public class UrlElement : ConfigurationElement
    {
        [ConfigurationProperty("url", IsKey = true, IsRequired = true)]
        public string Url
        {
            get { return (string)this["url"]; }
            set { base["url"] = value; }
        }
    }
}

Yet whenever I try to access my settings, like this:

var minWaitTime = int.Parse(ConfigurationManager.AppSettings["MinWaitTime"]);

I get a System.Configuration.ConfigurationErrorsException.

I've been looking at various examples online, but nothing seems to help in my case. What am I doing wrong?

Upvotes: 0

Views: 65

Answers (1)

Chris Bartlett
Chris Bartlett

Reputation: 315

Your problem is where your config section is declared. The configSections tag needs to be immediately after the configuration node. Try this:

   <configuration>
  <configSections>
    <section name="clientconfig" type="KeepWarm.ClientConfig" />
  </configSections>
  <appSettings>
    <add key="MinWaitTime" value="150" />
    <add key="MaxWaitTime" value="900" />
  </appSettings>
  <clientconfig username="test user" password="test password">
    <urls>
      <add url="test1" />
      <add url="test2" />
      <add url="test3" />
    </urls>
  </clientconfig>
</configuration>

Upvotes: 1

Related Questions