Search code examples
c#app-configconfigurationsection

String-ConfigurationProperty within custom ConfigurationSection


I've got the following ConfigurationSection in one of my libs in my solution (let's say LibA.dll):

public class MyConfigurationSection : ConfigurationSection
{
    [ConfigurationProperty("proxy", DefaultValue = "", IsRequired = false)]
    public string HttpProxy
    {
        get { return (string) this["proxy"]; }
        set { this["proxy"] = value; }
    }
}

In the App.config of my executable I'm using the Section.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="liba"
             type="LibA.MyConfigurationSection, LibA, Version=1.0.0.0, Culture=neutral"></section>
  </configSections>
  <liba>
    <proxy>abc</proxy>
  </liba>
</configuration>

First of all visual studio tells me that it can't find any schema. Secondly, the following line throws an exceptions:

MyConfigurationSection myConfiguration = (MyConfigurationSection) ConfigurationManager.GetSection("liba");

The proxy-Property is no ConfigurationElement.

Can't I use the type string within a Configuration section? Do I really have to create a custom ConfigurationElement?


Solution

  • As per your code the 'proxy' is an attribute of the 'liba' element. The xml should be in the following format

    <liba proxy="abc"></liba>