ConfigurationManager.GetSection(" sectionName")会为自定义节抛出ConfigurationErrorsException

时间:2015-10-01 15:04:06

标签: c# .net app-config

我在app.config文件中创建了一个自定义部分,但我无法检索它。我总是得到一个ConfigurationErrorsException。

public class UdpSettings : ConfigurationSection
{
    private static UdpSettings settings
      = ConfigurationManager.GetSection("UdpSettings") as UdpSettings;

    public static UdpSettings Settings
    {
        get
        {
            return settings;
        }
    }

    [ConfigurationProperty("puerto"
      , DefaultValue = 20
      , IsRequired = false)]
    [IntegerValidator(MinValue = 1
      , MaxValue = 65535)]
    public int Puerto
    {
        get { return (int)this["puerto"]; }
        set { this["puerto"] = value; }
    }

    [ConfigurationProperty("puertoTaconet"
      , DefaultValue = 20
      , IsRequired = false)]
    [IntegerValidator(MinValue = 1
      , MaxValue = 65535)]
    public int PuertoTaconet
    {
        get { return (int)this["puertoTaconet"]; }
        set { this["puertoTaconet"] = value; }
    }

    [ConfigurationProperty("rutaArchivoGeocerca"
     , IsRequired = true)]
    [StringValidator(InvalidCharacters = "  ~!@#$%^&*()[]{}/;’\"|"
      , MinLength = 1
      , MaxLength = 256)]
    public string RutaArchivoGeocerca
    {
        get { return (string)this["rutaArchivoGeocerca"]; }
        set { this["rutaArchivoGeocerca"] = value; }
    }


    [ConfigurationProperty("rutaArchivoConfig"
     , IsRequired = true)]
    [StringValidator(InvalidCharacters = "  ~!@#$%^&*()[]{}/;’\"|"
      , MinLength = 1
      , MaxLength = 256)]
    public string RutaArchivoConfig
    {
        get { return (string)this["rutaArchivoConfig"]; }
        set { this["rutaArchivoConfig"] = value; }
    }


    [ConfigurationProperty("rutaModem2"
     , IsRequired = true)]
    [StringValidator(InvalidCharacters = "  ~!@#$%^&*()[]{}/;’\"|"
      , MinLength = 1
      , MaxLength = 256)]
    public string RutaModem2
    {
        get { return (string)this["rutaModem2"]; }
        set { this["rutaModem2"] = value; }
    }

    [ConfigurationProperty("rutaModem1"
     , IsRequired = true)]
    [StringValidator(InvalidCharacters = "  ~!@#$%^&*()[]{}/;’\"|"
      , MinLength = 1
      , MaxLength = 256)]
    public string RutaModem1
    {
        get { return (string)this["rutaModem1"]; }
        set { this["rutaModem1"] = value; }
    }

    [ConfigurationProperty("rutaFirmwareEquipo"
      , IsRequired = true)]
    [StringValidator(InvalidCharacters = "  ~!@#$%^&*()[]{}/;’\"|"
      , MinLength = 1
      , MaxLength = 256)]
    public string RutaFirmwareEquipo
    {
        get { return (string)this["rutaFirmwareEquipo"]; }
        set { this["rutaFirmwareEquipo"] = value; }
    }
}

这是我的app.config:

<configuration>
  <configSections>
    <section name="UdpSettings" type="UDP_Taco.Modelo.UdpSettings" allowLocation="true" allowDefinition="Everywhere" />
  </configSections>
  <UdpSettings
    puerto ="1001"
    rutaArchivoGeocerca ="C:\Test"
    rutaArchivoConfig ="C:\Test"
    rutaModem2 ="C:\Test"
    rutaModem1 ="C:\Test"
    rutaFirmwareEquipo="C:\Test"
    puertoTaconet ="1015"/>
</configuration>

我觉得&#34;类型&#34;中出现了问题。我部分的属性。应该去哪里?

&#34; ConfigurationManager.GetSection(&#34; UdpSettings&#34)&#34;是抛出ConfigurationErrorsException的行。

1 个答案:

答案 0 :(得分:1)

您应该在type元素的section属性中指定完整的程序集限定类型名称。像这样:

<configSections>
    <section name="UdpSettings" type="UDP_Taco.Modelo.UdpSettings, UDP-Taxi, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" allowLocation="true" allowDefinition="Everywhere" />
</configSections>