我有一个可以连接到多个服务器的应用程序。直到运行时才知道实际的服务器数量,并且可能每天都在变化。完全定义服务器需要几个实际参数。
我正在尝试使用.NET支持应用程序配置来配置应用程序。
配置文件类似于:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup
name="userSettings"
type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="server"
type="System.Configuration.SingleTagSectionHandler"
allowExeDefinition="MachineToLocalUser"
requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<server name="washington">
<add name="host" value="abc.presidents.com"/>
<add name="port" value="1414"/>
<add name="credentials" value="george"/>
</server>
<server name="adams">
<add name="host" value="def.presidents.com"/>
<add name="port" value="1419"/>
<add name="credentials" value="john"/>
</server>
<!--insert more server definitions here -->
</userSettings>
</configuration>
我尝试使用类似的代码读取此内容(WriteLines用于诊断问题。如果它起作用,它们将消失。):
try
{
var exeConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
Console.WriteLine(exeConfiguration);
var userSettings = exeConfiguration.GetSectionGroup("userSettings");
Console.WriteLine("{0}: {1}", userSettings, userSettings.Name);
var sections = userSettings.Sections;
Console.WriteLine("{0}: {1}", sections, sections.Count);
foreach (ConfigurationSection section in sections)
{
Console.WriteLine("{0}", section);
// todo Here's where we capture information about a server
}
}
catch (System.Exception ex)
{
Console.WriteLine("Exception: {0}", ex.Message);
}
这将从foreach抛出异常并产生以下输出:
System.Configuration.Configuration
System.Configuration.UserSettingsGroup: userSettings
System.Configuration.ConfigurationSectionCollection: 1
Exception: Sections must only appear once per config file. See the help topic <location> for exceptions.
如果我删除第二台服务器,只留下华盛顿,那么它“正常”产生这个输出:
System.Configuration.Configuration
System.Configuration.ConfigurationSectionGroup: userSettings
System.Configuration.ConfigurationSectionCollection: 1
System.Configuration.DefaultSection
我在主题(嵌套的sectionGroups等)上尝试了其他变体而没有找到解决方案。我发现的各种教程示例似乎要我为每个服务器创建一个单独的类。这显然是不切实际的,并且应该是不必要的,因为所有服务器都是相同的。
问题:
基于Robert McKee的部分答案,我修改了xml,如下所示:
<section name="servers"
type="System.Configuration.DefaultSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
和
<servers>
<add name="washington" host="abc.presidents.com" port="1414" credentials="george"/>
<add name="adams" host="def.presidents.com" port="1419" credentials="john"/>
<!--insert more server definitions here -->
</servers>
这是一个致命问题的改进。正如您所看到的,我更改了type
元素的section
属性以命名类ystem.Configuration.DefaultSection
DefaultSection
成功读取配置信息(我认为,至少它没有抱怨。)但它没有暴露任何方式来访问它读取的信息!
因此我需要使用其他类型的*Section
类。 Microsoft提供了从ConfigurationSection
基类派生的95个类,但除DefaultSection
和ClientSettingsSection
之外的所有类似乎都针对特殊情况(URL,数据库连接字符串,日期和时间等) ...)ClientSettingsSection
甚至不会读取服务器部分 - 抱怨<add/>
不是有效的嵌套元素。
所以,最重要的是,我将不得不编写自定义ConfigurationSection
来处理这些设置。如果我开始工作,我会在最终解决方案中添加一个答案(除非有人先提供更好的答案。)
答案 0 :(得分:1)
Haven没有使用自定义配置部分,但从纯逻辑的角度来看,这不是更合适:
<userSettings>
<servers>
<add name="washington" host="abc.presidents.com" port="1414" credentials="george"/>
<add name="adams" host="def.presidents.com" port="1419" credentials="john"/>
<!--insert more server definitions here -->
</servers>
<!-- insert more user settings here -->
</userSettings>
您可以通过其他方式执行此操作,但如果您需要&#34;服务器&#34;的集合,则它必须位于&#34;服务器&#34;等分组元素中。你不能只拥有多个&#34;服务器&#34;在父元素中,也可以包含其他类型的子元素。组内的标签几乎总是&#34;添加&#34;。
在任何情况下&#34; System.Configuration.SingleTagSectionHandler&#34;绝对不是正确的类型。
答案 1 :(得分:1)
我最终得到的app.config文件是:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings"
type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="executiveBranch"
type="PropProto.Properties.ExecutiveBranchSection, PropProto, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
allowExeDefinition="MachineToLocalUser"
requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<executiveBranch>
<presidents>
<president key="first"
name="George Washington"
legacy="The Fother of Our County"
/>
<president key="honestAbe"
name="Abraham Lincoln"
legacy="Freed the Slaves"
/>
<president key="who"
name="Chester Arthur"
/>
<president key="dubya"
name="George W. Bush"
legacy="Mission Accomplished!!!"
/>
<president key="barack"
name="Barack Obama"
legacy="Affordable Health Care"
/>
</presidents>
</executiveBranch>
</userSettings>
</configuration>
还有一个嵌套级别比我预期的(或想要的)。那是因为presidents
是ConfigurationElementCollection
而userSettings
不能直接包含ConfigurationElementCollection
,所以我不得不介绍executiveBranch
。
阅读这些设置的代码是:
var exeConfiguration = ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.PerUserRoamingAndLocal);
var userSettings = exeConfiguration.GetSectionGroup("userSettings");
var executiveBranch = (ExecutiveBranchSection)userSettings.Sections.Get(
ExecutiveBranchSection.Tag);
var presidents = executiveBranch.Presidents;
foreach (President president in presidents)
{
Console.WriteLine("{0}: {1}", president.Name, president.Legacy);
}
自定义类可以完成所有这些工作:
public class ExecutiveBranchSection : ConfigurationSection
{
public const string Tag = "executiveBranch";
[ConfigurationProperty(PresidentCollection.Tag)]
public PresidentCollection Presidents { get { return (PresidentCollection)base[PresidentCollection.Tag]; } }
}
[ConfigurationCollection(typeof(President),
CollectionType = ConfigurationElementCollectionType.BasicMap,
AddItemName = President.Tag)]
public class PresidentCollection : ConfigurationElementCollection
{
public const string Tag = "presidents";
protected override string ElementName { get { return President.Tag; } }
public President this[int index]
{
get { return (President)base.BaseGet(index); }
set
{
if (base.BaseGet(index) != null)
{
base.BaseRemoveAt(index);
}
base.BaseAdd(index, value);
}
}
new public President this[string name] { get { return (President)base.BaseGet(name); } }
protected override ConfigurationElement CreateNewElement()
{
return new President();
}
protected override object GetElementKey(ConfigurationElement element)
{
return (element as President).Key;
}
}
public class President : ConfigurationElement
{
public const string Tag = "president";
[ConfigurationProperty("key", IsRequired = true)]
public string Key { get { return (string)base["key"]; } }
[ConfigurationProperty("name", IsRequired = true)]
public string Name { get { return (string)base["name"]; } }
[ConfigurationProperty("legacy", IsRequired = false)]
public string Legacy { get { return (string)base["legacy"]; } }
}