简体   繁体   English

ConfigurationManager.GetSection(“ sectionName”)为自定义节抛出ConfigurationErrorsException

[英]ConfigurationManager.GetSection(“sectionName”) throws ConfigurationErrorsException for custom section

I have made a custom section in my app.config file but I can't manage to retrieve it. 我已经在我的app.config文件中创建了一个自定义部分,但是我无法设法检索它。 I always get a ConfigurationErrorsException. 我总是收到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; }
    }
}

And this is my app.config: 这是我的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>

I'm feel there's something wrong in the "type" attribute of my section. 我觉得我的部分的“类型”属性有问题。 What should go in there? 那里应该放什么东西?

"ConfigurationManager.GetSection("UdpSettings")" is the line that throws ConfigurationErrorsException. “ ConfigurationManager.GetSection(” UdpSettings“)”是引发ConfigurationErrorsException的行。

You should specify a full assembly-qualified type name int a type attribute of a section element. 您应该在section元素的type属性中指定完整的程序集限定类型名称。 Like this: 像这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ConfigurationManager.GetSection(sectionName)在执行单元测试时返回null - ConfigurationManager.GetSection(sectionName) returns null while performing unit tests ConfigurationManager.GetSection不返回自定义部分 - ConfigurationManager.GetSection doesn't return custom section ConfigurationManager.GetSection()函数 - ConfigurationManager.GetSection() function ConfigurationManager.GetSection跳过重复项 - ConfigurationManager.GetSection Skips Duplicates ConfigurationManager.GetSection 返回 null - ConfigurationManager.GetSection returns null ConfigurationSection ConfigurationManager.GetSection()始终返回null - ConfigurationSection ConfigurationManager.GetSection() always returns null ReadOnlyNameValueCollection(从ConfigurationManager.GetSection读取) - ReadOnlyNameValueCollection (reading from ConfigurationManager.GetSection) ConfigurationManager.GetSection和Configuration.GetSection有什么区别? - What is Difference between ConfigurationManager.GetSection and Configuration.GetSection? 使用ConfigurationManager的OpenExeConfiguration和GetSection,(自定义配置文件和部分) - Using ConfigurationManager's OpenExeConfiguration and GetSection,(Custom config file and section) ConfigurationManager.GetSection始终为对象提供默认值 - ConfigurationManager.GetSection always gives object with default values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM