[英]ConfigurationManager.GetSection(sectionName) returns null while performing unit tests
[英]ConfigurationManager.GetSection(“sectionName”) throws ConfigurationErrorsException for custom section
我已经在我的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>
我觉得我的部分的“类型”属性有问题。 那里应该放什么东西?
“ ConfigurationManager.GetSection(” UdpSettings“)”是引发ConfigurationErrorsException的行。
您应该在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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.