![](/img/trans.png)
[英]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.