我没发现什么不对劲,但我得自己试试。这是我工作的版本,并与你的版本(包括缺失的部分)进行比较。如果您仍然看到发生了可怕的错误,可以将try/catch放在它调用GetSection的位置。
应用程序配置:
<configSections>
<section name="SAM.Configuration.Data" type="Test.DataConfigurationSection, Test" />
</configSections>
<SAM.Configuration.Data>
<serviceBroker sleepTime="1000">
<queues>
<queue name="TestQueue" priority="1"/>
</queues>
</serviceBroker>
</SAM.Configuration.Data>
配置类:
public class DataConfigurationSection : ConfigurationSection
{
public const string SectionName = "SAM.Configuration.Data";
private static DataConfigurationSection _configSection;
[ConfigurationProperty("serviceBroker", IsRequired = false)]
public ServiceBrokerConfigurationElement ServiceBroker
{
get
{
return (ServiceBrokerConfigurationElement)this["serviceBroker"];
}
}
public static DataConfigurationSection Section
{
get
{
if (_configSection == null)
{
_configSection = ((DataConfigurationSection) ConfigurationManager.GetSection(SectionName)));
}
return _configSection;
}
}
}
public class ServiceBrokerConfigurationElement : ConfigurationElement
{
[ConfigurationCollection(typeof(ServiceBrokerQueueElement), AddItemName = "queue")]
[ConfigurationProperty("queues", IsRequired = true)]
public ServiceBrokerQueueElementCollection Queues
{
get { return (ServiceBrokerQueueElementCollection)this["queues"]; }
}
[ConfigurationProperty("sleepTime", DefaultValue = (int)500, IsRequired = true)]
public int SleepTime
{
get { return (int)this["sleepTime"]; }
}
}
public class ServiceBrokerQueueElementCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new ServiceBrokerQueueElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((ServiceBrokerQueueElement)element).Name;
}
public new ServiceBrokerQueueElement this[string name]
{
get
{
return (ServiceBrokerQueueElement)this.BaseGet(name);
}
}
}
public class ServiceBrokerQueueElement : ConfigurationElement
{
[ConfigurationProperty("name", IsRequired = true)]
public string Name
{
get
{
return (string)this["name"];
}
set
{
this["name"] = value;
}
}
[ConfigurationProperty("priority", IsRequired = true)]
public int Priority
{
get
{
return (int)this["priority"];
}
set
{
this["priority"] = value;
}
}
}