本文整理汇总了C#中ConfigurationBuilder.Add方法的典型用法代码示例。如果您正苦于以下问题:C# ConfigurationBuilder.Add方法的具体用法?C# ConfigurationBuilder.Add怎么用?C# ConfigurationBuilder.Add使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ConfigurationBuilder
的用法示例。
在下文中一共展示了ConfigurationBuilder.Add方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: NewConfigurationProviderOverridesOldOneWhenKeyIsDuplicated
public void NewConfigurationProviderOverridesOldOneWhenKeyIsDuplicated()
{
// Arrange
var dic1 = new Dictionary<string, string>()
{
{"Key1:Key2", "ValueInMem1"}
};
var dic2 = new Dictionary<string, string>()
{
{"Key1:Key2", "ValueInMem2"}
};
var memConfigSrc1 = new MemoryConfigurationProvider(dic1);
var memConfigSrc2 = new MemoryConfigurationProvider(dic2);
var configurationBuilder = new ConfigurationBuilder();
// Act
configurationBuilder.Add(memConfigSrc1, load: false);
configurationBuilder.Add(memConfigSrc2, load: false);
var config = configurationBuilder.Build();
// Assert
Assert.Equal("ValueInMem2", config["Key1:Key2"]);
}
示例2: ArrayMerge
public void ArrayMerge()
{
var json1 = @"{
'ip': [
'1.2.3.4',
'7.8.9.10',
'11.12.13.14'
]
}";
var json2 = @"{
'ip': {
'3': '15.16.17.18'
}
}";
var jsonConfigSource1 = new JsonConfigurationProvider(TestStreamHelpers.ArbitraryFilePath);
jsonConfigSource1.Load(TestStreamHelpers.StringToStream(json1));
var jsonConfigSource2 = new JsonConfigurationProvider(TestStreamHelpers.ArbitraryFilePath);
jsonConfigSource2.Load(TestStreamHelpers.StringToStream(json2));
var builder = new ConfigurationBuilder();
builder.Add(jsonConfigSource1, load: false);
builder.Add(jsonConfigSource2, load: false);
var config = builder.Build();
Assert.Equal(4, config.GetSection("ip").GetChildren().Count());
Assert.Equal("1.2.3.4", config["ip:0"]);
Assert.Equal("7.8.9.10", config["ip:1"]);
Assert.Equal("11.12.13.14", config["ip:2"]);
Assert.Equal("15.16.17.18", config["ip:3"]);
}
示例3: CanGetConfigurationSection
public void CanGetConfigurationSection()
{
// Arrange
var dic1 = new Dictionary<string, string>()
{
{"Data:DB1:Connection1", "MemVal1"},
{"Data:DB1:Connection2", "MemVal2"}
};
var dic2 = new Dictionary<string, string>()
{
{"DataSource:DB2:Connection", "MemVal3"}
};
var dic3 = new Dictionary<string, string>()
{
{"Data", "MemVal4"}
};
var memConfigSrc1 = new MemoryConfigurationSource(dic1);
var memConfigSrc2 = new MemoryConfigurationSource(dic2);
var memConfigSrc3 = new MemoryConfigurationSource(dic3);
var builder = new ConfigurationBuilder();
builder.Add(memConfigSrc1, load: false);
builder.Add(memConfigSrc2, load: false);
builder.Add(memConfigSrc3, load: false);
var config = builder.Build();
string memVal1, memVal2, memVal3, memVal4, memVal5;
// Act
var configFocus = config.GetSection("Data");
memVal1 = configFocus["DB1:Connection1"];
memVal2 = configFocus["DB1:Connection2"];
memVal3 = configFocus["DB2:Connection"];
memVal4 = configFocus["Source:DB2:Connection"];
memVal5 = configFocus.Value;
// Assert
Assert.Equal("MemVal1", memVal1);
Assert.Equal("MemVal2", memVal2);
Assert.Equal("MemVal4", memVal5);
Assert.Equal("MemVal1", configFocus["DB1:Connection1"]);
Assert.Equal("MemVal2", configFocus["DB1:Connection2"]);
Assert.Null(configFocus["DB2:Connection"]);
Assert.Null(configFocus["Source:DB2:Connection"]);
Assert.Equal("MemVal4", configFocus.Value);
}
示例4: LoadAndCombineKeyValuePairsFromDifferentConfigurationProviders
public void LoadAndCombineKeyValuePairsFromDifferentConfigurationProviders()
{
// Arrange
var dic1 = new Dictionary<string, string>()
{
{"Mem1:KeyInMem1", "ValueInMem1"}
};
var dic2 = new Dictionary<string, string>()
{
{"Mem2:KeyInMem2", "ValueInMem2"}
};
var dic3 = new Dictionary<string, string>()
{
{"Mem3:KeyInMem3", "ValueInMem3"}
};
var memConfigSrc1 = new MemoryConfigurationProvider(dic1);
var memConfigSrc2 = new MemoryConfigurationProvider(dic2);
var memConfigSrc3 = new MemoryConfigurationProvider(dic3);
var configurationBuilder = new ConfigurationBuilder();
// Act
configurationBuilder.Add(memConfigSrc1, load: false);
configurationBuilder.Add(memConfigSrc2, load: false);
configurationBuilder.Add(memConfigSrc3, load: false);
var config = configurationBuilder.Build();
var memVal1 = config["mem1:keyinmem1"];
var memVal2 = config["Mem2:KeyInMem2"];
var memVal3 = config["MEM3:KEYINMEM3"];
// Assert
Assert.Contains(memConfigSrc1, configurationBuilder.Providers);
Assert.Contains(memConfigSrc2, configurationBuilder.Providers);
Assert.Contains(memConfigSrc3, configurationBuilder.Providers);
Assert.Equal("ValueInMem1", memVal1);
Assert.Equal("ValueInMem2", memVal2);
Assert.Equal("ValueInMem3", memVal3);
Assert.Equal("ValueInMem1", config["mem1:keyinmem1"]);
Assert.Equal("ValueInMem2", config["Mem2:KeyInMem2"]);
Assert.Equal("ValueInMem3", config["MEM3:KEYINMEM3"]);
Assert.Null(config["NotExist"]);
}
示例5: CanGetConfigurationChildren
public void CanGetConfigurationChildren()
{
// Arrange
var dic1 = new Dictionary<string, string>()
{
{"Data:DB1:Connection1", "MemVal1"},
{"Data:DB1:Connection2", "MemVal2"}
};
var dic2 = new Dictionary<string, string>()
{
{"Data:DB2Connection", "MemVal3"}
};
var dic3 = new Dictionary<string, string>()
{
{"DataSource:DB3:Connection", "MemVal4"}
};
var memConfigSrc1 = new MemoryConfigurationProvider(dic1);
var memConfigSrc2 = new MemoryConfigurationProvider(dic2);
var memConfigSrc3 = new MemoryConfigurationProvider(dic3);
var builder = new ConfigurationBuilder();
builder.Add(memConfigSrc1, load: false);
builder.Add(memConfigSrc2, load: false);
builder.Add(memConfigSrc3, load: false);
var config = builder.Build();
// Act
var configSections = config.GetSection("Data").GetChildren().ToList();
// Assert
Assert.Equal(2, configSections.Count());
Assert.Equal("MemVal1", configSections.FirstOrDefault(c => c.Key == "DB1")["Connection1"]);
Assert.Equal("MemVal2", configSections.FirstOrDefault(c => c.Key == "DB1")["Connection2"]);
Assert.Equal("MemVal3", configSections.FirstOrDefault(c => c.Key == "DB2Connection").Value);
Assert.False(configSections.Exists(c => c.Key == "DB3"));
Assert.False(configSections.Exists(c => c.Key == "DB3"));
}
示例6: SettingValueUpdatesAllConfigurationSources
public void SettingValueUpdatesAllConfigurationSources()
{
// Arrange
var dict = new Dictionary<string, string>()
{
{"Key1", "Value1"},
{"Key2", "Value2"}
};
var memConfigSrc1 = new MemoryConfigurationSource(dict);
var memConfigSrc2 = new MemoryConfigurationSource(dict);
var memConfigSrc3 = new MemoryConfigurationSource(dict);
var builder = new ConfigurationBuilder();
builder.Add(memConfigSrc1, load: false);
builder.Add(memConfigSrc2, load: false);
builder.Add(memConfigSrc3, load: false);
var config = builder.Build();
// Act
config["Key1"] = "NewValue1";
config["Key2"] = "NewValue2";
// Assert
Assert.Equal("NewValue1", config["Key1"]);
Assert.Equal("NewValue1", memConfigSrc1.Get("Key1"));
Assert.Equal("NewValue1", memConfigSrc2.Get("Key1"));
Assert.Equal("NewValue1", memConfigSrc3.Get("Key1"));
Assert.Equal("NewValue2", config["Key2"]);
Assert.Equal("NewValue2", memConfigSrc1.Get("Key2"));
Assert.Equal("NewValue2", memConfigSrc2.Get("Key2"));
Assert.Equal("NewValue2", memConfigSrc3.Get("Key2"));
}
示例7: CanIterateWithGenericEnumerator
public void CanIterateWithGenericEnumerator()
{
// Arrange
var dict = new Dictionary<string, string>()
{
{"Mem:KeyInMem", "MemVal"}
};
var memConfigSrc1 = new MemoryConfigurationSource(dict);
var memConfigSrc2 = new MemoryConfigurationSource(dict);
var memConfigSrc3 = new MemoryConfigurationSource(dict);
var srcSet = new HashSet<IConfigurationSource>()
{
memConfigSrc1,
memConfigSrc2,
memConfigSrc3
};
var builder = new ConfigurationBuilder();
// Act
builder.Add(memConfigSrc1, load: false);
builder.Add(memConfigSrc2, load: false);
builder.Add(memConfigSrc3, load: false);
var config = builder.Build();
// Assert
var enumerator = builder.Sources.GetEnumerator();
int srcCount = 0;
while (enumerator.MoveNext())
{
Assert.Contains(enumerator.Current, srcSet);
++srcCount;
}
Assert.Equal(3, srcCount);
}
示例8: ArraysAreKeptInFileOrder
public void ArraysAreKeptInFileOrder()
{
var json = @"{
'setting': [
'b',
'a',
'2'
]
}";
var jsonConfigSource = new JsonConfigurationProvider(TestStreamHelpers.ArbitraryFilePath);
jsonConfigSource.Load(TestStreamHelpers.StringToStream(json));
var builder = new ConfigurationBuilder();
builder.Add(jsonConfigSource, load: false);
var config = builder.Build();
var configurationSection = config.GetSection("setting");
var indexConfigurationSections = configurationSection.GetChildren().ToArray();
Assert.Equal(3, indexConfigurationSections.Count());
Assert.Equal("b", indexConfigurationSections[0].Value);
Assert.Equal("a", indexConfigurationSections[1].Value);
Assert.Equal("2", indexConfigurationSections[2].Value);
}
示例9: PropertiesAreSortedByNumberOnlyFirst
public void PropertiesAreSortedByNumberOnlyFirst()
{
var json = @"{
'setting': {
'hello': 'a',
'bob': 'b',
'42': 'c',
'4':'d',
'10': 'e',
'1text': 'f',
}
}";
var jsonConfigSource = new JsonConfigurationProvider(TestStreamHelpers.ArbitraryFilePath);
jsonConfigSource.Load(TestStreamHelpers.StringToStream(json));
var builder = new ConfigurationBuilder();
builder.Add(jsonConfigSource, load: false);
var config = builder.Build();
var configurationSection = config.GetSection("setting");
var indexConfigurationSections = configurationSection.GetChildren().ToArray();
Assert.Equal(6, indexConfigurationSections.Count());
Assert.Equal("4", indexConfigurationSections[0].Key);
Assert.Equal("10", indexConfigurationSections[1].Key);
Assert.Equal("42", indexConfigurationSections[2].Key);
Assert.Equal("1text", indexConfigurationSections[3].Key);
Assert.Equal("bob", indexConfigurationSections[4].Key);
Assert.Equal("hello", indexConfigurationSections[5].Key);
}
示例10: SourcesReturnsAddedConfigurationProviders
public void SourcesReturnsAddedConfigurationProviders()
{
// Arrange
var dict = new Dictionary<string, string>()
{
{"Mem:KeyInMem", "MemVal"}
};
var memConfigSrc1 = new MemoryConfigurationProvider(dict);
var memConfigSrc2 = new MemoryConfigurationProvider(dict);
var memConfigSrc3 = new MemoryConfigurationProvider(dict);
var srcSet = new HashSet<IConfigurationProvider>()
{
memConfigSrc1,
memConfigSrc2,
memConfigSrc3
};
var configurationBuilder = new ConfigurationBuilder();
// Act
configurationBuilder.Add(memConfigSrc1, load: false);
configurationBuilder.Add(memConfigSrc2, load: false);
configurationBuilder.Add(memConfigSrc3, load: false);
var config = configurationBuilder.Build();
// Assert
Assert.Equal(new[] { memConfigSrc1, memConfigSrc2, memConfigSrc3 }, configurationBuilder.Providers);
}
示例11: ImplicitArrayItemReplacement
public void ImplicitArrayItemReplacement()
{
var json1 = @"{
'ip': [
'1.2.3.4',
'7.8.9.10',
'11.12.13.14'
]
}";
var json2 = @"{
'ip': [
'15.16.17.18'
]
}";
var jsonConfigSource1 = new JsonConfigurationSource(TestStreamHelpers.ArbitraryFilePath);
jsonConfigSource1.Load(TestStreamHelpers.StringToStream(json1));
var jsonConfigSource2 = new JsonConfigurationSource(TestStreamHelpers.ArbitraryFilePath);
jsonConfigSource2.Load(TestStreamHelpers.StringToStream(json2));
var builder = new ConfigurationBuilder();
builder.Add(jsonConfigSource1, load: false);
builder.Add(jsonConfigSource2, load: false);
var config = builder.Build();
Assert.Equal(3, config.GetConfigurationSections("ip").Count());
Assert.Equal("15.16.17.18", config.Get("ip:0"));
Assert.Equal("7.8.9.10", config.Get("ip:1"));
Assert.Equal("11.12.13.14", config.Get("ip:2"));
}
示例12: CanGetConfigurationSections
public void CanGetConfigurationSections()
{
// Arrange
var dic1 = new Dictionary<string, string>()
{
{"Data:DB1:Connection1", "MemVal1"},
{"Data:DB1:Connection2", "MemVal2"}
};
var dic2 = new Dictionary<string, string>()
{
{"Data:DB2Connection", "MemVal3"}
};
var dic3 = new Dictionary<string, string>()
{
{"DataSource:DB3:Connection", "MemVal4"}
};
var memConfigSrc1 = new MemoryConfigurationSource(dic1);
var memConfigSrc2 = new MemoryConfigurationSource(dic2);
var memConfigSrc3 = new MemoryConfigurationSource(dic3);
var builder = new ConfigurationBuilder();
builder.Add(memConfigSrc1, load: false);
builder.Add(memConfigSrc2, load: false);
builder.Add(memConfigSrc3, load: false);
var config = builder.Build();
// Act
var configFocusList = config.GetConfigurationSections("Data");
var configurationSectionsSet = configFocusList.ToDictionary(e => e.Key, e => e.Value);
// Assert
Assert.Equal(2, configFocusList.Count());
Assert.Equal("MemVal1", configurationSectionsSet["DB1"].Get("Connection1"));
Assert.Equal("MemVal2", configurationSectionsSet["DB1"].Get("Connection2"));
Assert.Equal("MemVal3", configurationSectionsSet["DB2Connection"].Get(null));
Assert.False(configurationSectionsSet.ContainsKey("DB3"));
Assert.False(configurationSectionsSet.ContainsKey("Source:DB3"));
}
示例13: LoadAndCombineKeyValuePairsFromDifferentConfigurationSources
public void LoadAndCombineKeyValuePairsFromDifferentConfigurationSources()
{
// Arrange
var dic1 = new Dictionary<string, string>()
{
{"Mem1:KeyInMem1", "ValueInMem1"}
};
var dic2 = new Dictionary<string, string>()
{
{"Mem2:KeyInMem2", "ValueInMem2"}
};
var dic3 = new Dictionary<string, string>()
{
{"Mem3:KeyInMem3", "ValueInMem3"}
};
var memConfigSrc1 = new MemoryConfigurationSource(dic1);
var memConfigSrc2 = new MemoryConfigurationSource(dic2);
var memConfigSrc3 = new MemoryConfigurationSource(dic3);
var builder = new ConfigurationBuilder();
string memVal1, memVal2, memVal3;
bool memRet1, memRet2, memRet3;
// Act
builder.Add(memConfigSrc1, load: false);
builder.Add(memConfigSrc2, load: false);
builder.Add(memConfigSrc3, load: false);
var config = builder.Build();
memRet1 = config.TryGet("mem1:keyinmem1", out memVal1);
memRet2 = config.TryGet("Mem2:KeyInMem2", out memVal2);
memRet3 = config.TryGet("MEM3:KEYINMEM3", out memVal3);
// Assert
Assert.Contains(memConfigSrc1, builder.Sources);
Assert.Contains(memConfigSrc2, builder.Sources);
Assert.Contains(memConfigSrc3, builder.Sources);
Assert.True(memRet1);
Assert.True(memRet2);
Assert.True(memRet3);
Assert.Equal("ValueInMem1", memVal1);
Assert.Equal("ValueInMem2", memVal2);
Assert.Equal("ValueInMem3", memVal3);
Assert.Equal("ValueInMem1", config.Get("mem1:keyinmem1"));
Assert.Equal("ValueInMem2", config.Get("Mem2:KeyInMem2"));
Assert.Equal("ValueInMem3", config.Get("MEM3:KEYINMEM3"));
Assert.Null(config.Get("NotExist"));
Assert.Equal("ValueInMem1", config["mem1:keyinmem1"]);
Assert.Equal("ValueInMem2", config["Mem2:KeyInMem2"]);
Assert.Equal("ValueInMem3", config["MEM3:KEYINMEM3"]);
Assert.Null(config["NotExist"]);
}
示例14: CanGetConfigurationSection
public void CanGetConfigurationSection()
{
// Arrange
var dic1 = new Dictionary<string, string>()
{
{"Data:DB1:Connection1", "MemVal1"},
{"Data:DB1:Connection2", "MemVal2"}
};
var dic2 = new Dictionary<string, string>()
{
{"DataSource:DB2:Connection", "MemVal3"}
};
var dic3 = new Dictionary<string, string>()
{
{"Data", "MemVal4"}
};
var memConfigSrc1 = new MemoryConfigurationSource(dic1);
var memConfigSrc2 = new MemoryConfigurationSource(dic2);
var memConfigSrc3 = new MemoryConfigurationSource(dic3);
var builder = new ConfigurationBuilder();
builder.Add(memConfigSrc1, load: false);
builder.Add(memConfigSrc2, load: false);
builder.Add(memConfigSrc3, load: false);
var config = builder.Build();
string memVal1, memVal2, memVal3, memVal4, memVal5;
bool memRet1, memRet2, memRet3, memRet4, memRet5;
// Act
var configFocus = config.GetConfigurationSection("Data");
memRet1 = configFocus.TryGet("DB1:Connection1", out memVal1);
memRet2 = configFocus.TryGet("DB1:Connection2", out memVal2);
memRet3 = configFocus.TryGet("DB2:Connection", out memVal3);
memRet4 = configFocus.TryGet("Source:DB2:Connection", out memVal4);
memRet5 = configFocus.TryGet(null, out memVal5);
// Assert
Assert.True(memRet1);
Assert.True(memRet2);
Assert.False(memRet3);
Assert.False(memRet4);
Assert.True(memRet5);
Assert.Equal("MemVal1", memVal1);
Assert.Equal("MemVal2", memVal2);
Assert.Equal("MemVal4", memVal5);
Assert.Equal("MemVal1", configFocus.Get("DB1:Connection1"));
Assert.Equal("MemVal2", configFocus.Get("DB1:Connection2"));
Assert.Null(configFocus.Get("DB2:Connection"));
Assert.Null(configFocus.Get("Source:DB2:Connection"));
Assert.Equal("MemVal4", configFocus.Get(null));
Assert.Equal("MemVal1", configFocus["DB1:Connection1"]);
Assert.Equal("MemVal2", configFocus["DB1:Connection2"]);
Assert.Null(configFocus["DB2:Connection"]);
Assert.Null(configFocus["Source:DB2:Connection"]);
Assert.Equal("MemVal4", configFocus[null]);
}