我正在使用System.Configuration.ConfigurationElement来表示app.config中的一些数据。它正确检测到该部分在那里(如果该部分不是它会抛出错误),但它从未正确填充。对于队列我只得到一个空集合,而对于sleepTime,我只得到500。
这是相关的app.config部分:
<serviceBroker sleepTime="1000">
<queues>
<queue name="TestQueue" priority="1"/>
</queues>
</serviceBroker>
这是完整的app.config
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler,Castle.Windsor" />
<section name="SAM.Configuration.Data" type="SAM.Configuration.Data.DataConfigurationSection, SAM.Configuration"/>
<section name="NHibernate.Caches.MemCache" type="NHibernate.Caches.MemCache.MemCacheSectionHandler,NHibernate.Caches.MemCache" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<castle/>
<connectionStrings>
<remove name="LocalSqlServer"/>
<add name="LocalSqlServer" connectionString=""/>
<add name="AuthSqlServer" connectionString=""/>
</connectionStrings>
<SAM.Configuration.Data>
<mage MaxThreads="1" SmtpHost="qa" SmtpPort="25">
<Tasks/>
</mage>
<serviceBroker sleepTime="1000">
<queues>
<queue name="TestQueue" priority="1"/>
</queues>
</serviceBroker>
<databases>
<database name="BusinessDomain" defaultConnectionName="">
<connections>
<connection>
<add key="name" value=""/>
<add key="assembly" value="SAM.Data.NHibernateProvider"/>
<add key="type" value="SAM.Data.NHibernateProvider.RepositoryManager"/>
<add key="configurationFile" value="Business.NHibernateSession.Config"/>
<add key="isEncrypted" value="FALSE" />
</connection>
<connection>
<add key="name" value=""/>
<add key="assembly" value="SAM.Data.NHibernateProvider"/>
<add key="type" value="SAM.Data.NHibernateProvider.RepositoryManager"/>
<add key="configurationFile" value="Business.NHibernateSession.Config"/>
<add key="isEncrypted" value="FALSE" />
</connection>
</connections>
</database>
</databases>
</SAM.Configuration.Data>
<NHibernate.Caches.MemCache configSource="nhibernate.memcache.config" />
</configuration>
这是我的ConfigurationElement:
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"]; }
}
}
这是我的ConfigurationSection(我已经删除了许多其他正常工作的元素)
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;
}
}
}
答案 0 :(得分:0)
您能否验证您的应用程序配置文件是否包含相关的配置部分声明?将配置部分映射到实现它的类型需要类似以下内容。
<configuration>
<configSections>
<section name="SamConfigurationData" type="Namespace.DataConfigurationSection, AssemblyName"/>
</configSections>
<SamConfigurationData>
<serviceBroker sleepTime="1000">
<queues>
<queue name="TestQueue" priority="1"/>
</queues>
</serviceBroker>
</SamConfigurationData>
</configuration>
答案 1 :(得分:0)
我没有看到任何错误,但我必须亲自尝试。这是我工作的版本,并将其与您拥有的版本(包括缺失的部分)进行比较。如果你仍然看到一些可怕的错误发生,你可以把一个try / catch放在它调用GetSection的地方。
的app.config:
<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;
}
}
}
答案 2 :(得分:0)
它基本上与配置部分没什么关系,我在Castle.Windsor中错误地连接了所有内容。它始终返回ServiceBrokerConfiguration的空实例,而不是从app.config加载的实例。
感谢大家的帮助。