如何解决app.config中的多个相同的自定义配置部分
| 我正在尝试在C#.NET控制台应用程序的app.config文件中创建自定义配置部分。它将存储有关某些服务器的一些详细信息,例如:<configSections>
<sectionGroup name=\"serverGroup\">
<section name=\"server\" type=\"RPInstaller.ServerConfig\" allowLocation=\"true\" allowDefinition=\"Everywhere\"/>
</sectionGroup>
</configSections>
<serverGroup>
<server>
<name>rmso2srvm</name>
<isBatchServer>false</isBatchServer>
</server>
<server>
<name>rmsb2srvm</name>
<isBatchServer>true</isBatchServer>
</server>
</serverGroup>
我为服务器部分定义了一个类,如下所示:
namespace RPInstaller
{
public class ServerConfig : ConfigurationSection
{
[ConfigurationProperty(\"name\",IsRequired=true)]
public string Name {...}
[ConfigurationProperty(\"isBatchServer\",IsRequired = true)]
public bool IsBatchServer {...}
}
}
现在,当我尝试加载服务器节时,出现一个异常:\“节只能在每个配置文件中出现一次\”。
如何在我的app.config文件中合法定义多个服务器部分?
解决方法
<confgisections>
<section name=\"server\" type=\"RPInstaller.ServerConfig\" allowLocation=\"true\" allowDefinition=\"Everywhere\"/>
</confgisections>
<server>
<servers>
</clear>
<add name=\"rmso2srvm\" isBatchServer=\"false\"/>
<add name=\"rmsb2srvm\" isBatchServer=\"true\"/>
</servers>
</server>
我以前是如何设置自定义部分的
要访问的VB代码:
Dim cfg As ServerSection = (ConfigurationManager.GetSection(\"Server\"),ServerSection)
cfg.ServersCollection(\"nameOfServer\").isBatchServer
,您无法在一个web.config中创建多个服务器部分。
自定义部分中只有多个元素。
检查您的web.config-似乎是由于您的代码引起的错误。
更新:
您没有为\“ server \”元素定义元素-仅为ConfigurationSection。
因此,运行时等待像
rmso2srvm
假
您应该添加ServerElement : ConfigurationElement
类,并将其添加到您的section类的定义中:
namespace RPInstaller
{
public class ServerConfig : ConfigurationSection
{
public class ServerElement : ConfigurationElement
{
[ConfigurationProperty(\"name\",IsRequired=true)]
public string Name {...}
[ConfigurationProperty(\"isBatchServer\",IsRequired = true)]
public bool IsBatchServer {...}
}
}
}
此处有更多信息:
http://msdn.microsoft.com/zh-CN/library/2tw134k3.aspx
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。