getsection ("sectionName")为自定义节抛出ConfigurationErro

本文关键字:quot ConfigurationErro 自定义 sectionName getsection | 更新日期: 2023-09-27 18:15:26

我已经在我的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>

我觉得我的section的"type"属性有问题。里面应该放什么?

"ConfigurationManager.GetSection("UdpSettings")"是抛出ConfigurationErrorsException的那行。

getsection ("sectionName")为自定义节抛出ConfigurationErro

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

<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>