whi*_*key 11 c# xml configuration app-config visual-studio
我必须在根设置组中保存2组不同的设置.它应该是这样的:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="ROOT_GROUP">
<sectionGroup name="GROUP_1">
........................
some_settings
........................
</sectionGroup>
<sectionGroup name="GROUP_2">
........................
some_other_settings
........................
</sectionGroup>
</sectionGroup>
</configSections>
................................
other_system_tags
................................
</configuration>
Run Code Online (Sandbox Code Playgroud)
Nuance是我必须在我的代码中的不同位置一个接一个地保存它.(例如,GROUP_1可以是连接字符串,GROUP_2是一些环境设置,它们一起由我的应用程序的不同部分中的用户填充)
我制作了这个简单的测试类来获得预期的结果
[TestFixture]
public class Tttt
{
private string ROOT_GROUP = "ROOT_GROUP";
private string GROUP_1 = "GROUP_1";
private string GROUP_2 = "GROUP_2";
[Test]
public void SaveSettingsGroups()
{
SaveGroup1();
SaveGroup2();
Assert.True(true);
}
private Configuration GetConfig()
{
var configFilePath = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
var map = new ExeConfigurationFileMap { ExeConfigFilename = configFilePath };
var config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
return config;
}
private void SaveGroup1()
{
var config = GetConfig();
var root = new UserSettingsGroup();
config.SectionGroups.Add(ROOT_GROUP, root);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(root.Name);
var nested = new UserSettingsGroup();
root.SectionGroups.Add(GROUP_1, nested);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(nested.Name);
}
private void SaveGroup2()
{
var config = GetConfig();
var root = config.GetSectionGroup(ROOT_GROUP);
var nested = new UserSettingsGroup();
root.SectionGroups.Add(GROUP_2, nested);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(nested.Name);
}
}
Run Code Online (Sandbox Code Playgroud)
但由于某种原因,此代码的结果是不同的
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="ROOT_GROUP">
<sectionGroup name="GROUP_1">
........................
some_settings
........................
</sectionGroup>
</sectionGroup>
<sectionGroup name="ROOT_GROUP">
<sectionGroup name="GROUP_2">
........................
some_other_settings
........................
</sectionGroup>
</sectionGroup>
</configSections>
................................
other_system_tags
................................
</configuration>
Run Code Online (Sandbox Code Playgroud)
ROOT_GROUP节点是重复的,当然visual studio会抛出ROOT_GROUP已存在的异常.显然,当我将新的嵌套组添加到现有的根组然后保存它时,我的问题隐藏在方法SaveGroup2()中 - 但为什么呢?
UPD 我刚刚添加了新方法
private void SaveGroup3()
{
var config = GetConfig();
var root = config.GetSectionGroup(ROOT_GROUP);
var nested1 = root.SectionGroups.Get(0);
var nested2 = new UserSettingsGroup();
var nested3 = new UserSettingsGroup();
nested1.SectionGroups.Add("GROUP_2", nested2);
root.SectionGroups.Add("GROUP_3", nested3);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(nested2.Name);
ConfigurationManager.RefreshSection(nested3.Name);
}
Run Code Online (Sandbox Code Playgroud)
并在测试中替换它
[Test]
public void SaveSettingsGroups()
{
SaveGroup1();
SaveGroup3();
Assert.True(true);
}
Run Code Online (Sandbox Code Playgroud)
并得到了这种奇怪的行为
<sectionGroup name="ROOT_GROUP">
<sectionGroup name="GROUP_1">
<sectionGroup name="GROUP_2">
</sectionGroup>
</sectionGroup>
<sectionGroup name="GROUP_3">
</sectionGroup>
</sectionGroup>
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,奇怪的是结果完全是预期的.ROOT_GROUP没有重复,因为我需要它,但为什么它在SaveGroup2()中呢?我在SaveGroup2()中遗漏了什么吗?
UPD2 - HACK
刚试了一个简单的想法 - 如果我在添加一个新的嵌套元素之前清除root_group怎么办?
private void SaveGroup2()
{
var config = GetConfig();
var root = config.GetSectionGroup(ROOT_GROUP);
var nested = new ConfigurationSectionGroup();
//Copy exiting nested groups to array
var gr = new ConfigurationSectionGroup[5];
root.SectionGroups.CopyTo(gr,0);
gr[1] = nested;
//<!----
root.SectionGroups.Clear();
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(root.Name);
root.SectionGroups.Add(gr[0].Name, gr[0]);
root.SectionGroups.Add(GROUP_2, gr[1]);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(root.Name);
}
Run Code Online (Sandbox Code Playgroud)
你怎么猜 - 它的作用!
<sectionGroup name="ROOT_GROUP">
<sectionGroup name="GROUP_1" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
</sectionGroup>
<sectionGroup name="GROUP_2" type="System.Configuration.ConfigurationSectionGroup, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" >
</sectionGroup>
</sectionGroup>
Run Code Online (Sandbox Code Playgroud)
我认为它看起来像一个bug或者有一些我错过的隐藏的东西.有人可以解释一下我做错了什么吗?
我花了一段时间才弄清楚发生了什么,在我看来,框架代码本身有问题,特别是class 内部的方法 。我不想写长篇大论,但如果您愿意,您可以调试 .Net 框架代码并亲自查看。WriteUnwrittenConfigDeclarationsRecursive(SectionUpdates declarationUpdates, XmlUtilWriter utilWriter, int linePosition, int indent, bool skipFirstIndent)MgmtConfigurationRecord
您可以通过以下方式修复代码:
1. 将所有组保存在一起
private void SaveGroups()
{
var config = GetConfig();
var root = new ConfigurationSectionGroup();
config.SectionGroups.Add(ROOT_GROUP, root);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(root.Name);
var nested = new UserSettingsGroup();
root.SectionGroups.Add(GROUP_1, nested);
nested = new UserSettingsGroup();
root.SectionGroups.Add(GROUP_2, nested);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(root.Name);
}
Run Code Online (Sandbox Code Playgroud)
2. 在添加新组项目之前删除现有组项目
private void SaveGroup2()
{
var config = GetConfig();
var root = config.SectionGroups[ROOT_GROUP];
var existingGroups = new Dictionary<string, ConfigurationSectionGroup>();
while (root.SectionGroups.Count > 0)
{
existingGroups.Add(root.SectionGroups.Keys[0], root.SectionGroups[0]);
root.SectionGroups.RemoveAt(0);
}
config.Save(ConfigurationSaveMode.Modified);
existingGroups.Add(GROUP_2, new UserSettingsGroup());
foreach (var key in existingGroups.Keys)
{
existingGroups[key].ForceDeclaration(true);
root.SectionGroups.Add(key, existingGroups[key]);
}
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(root.Name);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
184 次 |
| 最近记录: |