YamlConfigurationProvider
YamlConfigurationProvider represents a .yaml file as configuration source.
// IConfigurationSource
var configurationSource = new YamlConfigurationProvider.Source
{
FileProvider = new PhysicalFileProvider(Directory.GetCurrentDirectory()),
Path = "appsettings.yaml",
ReloadOnChange = true,
Optional = true
};
// IConfigurationProvider
IConfigurationProvider configurationProvider = new YamlConfigurationProvider(configurationSource);
// Create configuration
IConfiguration configuration = new ConfigurationBuilder()
.Add(configurationSource)
.Build();
YamlConfigurationExtensions
.AddYamlFile(path, optional, reloadOnChange) extension method adds a yaml file to IConfigurationBuilder.
// Create configuration
IConfiguration configuration = new ConfigurationBuilder()
.AddYamlFile("appsettings.yaml", optional: true, reloadOnChange: true)
.Build();
Same extension method works with IHostBuilder.
// Create host
IHost host =
new HostBuilder()
.ConfigureDefaults(null)
.ConfigureAppConfiguration(cb => cb.AddYamlFile("appsettings.yaml", optional: true, reloadOnChange: true))
.Build();
// Get configuration
IConfiguration configuration = host.Services.GetRequiredService<IConfiguration>();
YamlNodeExtensions
.ToYaml(properties, policy, targetRoot?, delimiter) is creates a YamlNode to match contents of 'properties'.
// Create properties
var properties = new Dictionary<string, string?>
{
{ "Cars:0:License", "ABC-123" },
{ "Cars:0:Owner", "Donald" },
{ "Cars:0:PreviousOwners:0", "Mickey" },
{ "Cars:0:PreviousOwners:1", "Goofy" },
{ "Cars:1:License", "XYZ-456" },
{ "Cars:1:Owner", "Mickey" },
{ "Cars:1:PreviousOwners:0", "Pluto" },
{ "Cars:2:License", "YYY-444" },
{ "Cars:2:Owner", "Scrooge" },
};
// Convert 'properties' to a YamlNode
YamlNode root = YamlNodeExtensions.ToYaml(
properties: properties,
policy: YamlNodeExtensions.CorrelatePolicy.RemoveUnusedNodes | YamlNodeExtensions.CorrelatePolicy.RemoveAmbiguentNodes,
targetRoot: null,
delimiter: ':');
// Convert to yaml document
var stringWriter = new StringWriter();
YamlDocument yamlDocument = new YamlDocument(root);
YamlStream yamlStream = new YamlStream(yamlDocument);
yamlStream.Save(stringWriter, assignAnchors: false);
string text = stringWriter.ToString();
// Print
Console.WriteLine(text);
Cars:
- License: ABC-123
Owner: Donald
PreviousOwners:
- Mickey
- Goofy
- License: XYZ-456
Owner: Mickey
PreviousOwners:
- Pluto
- License: YYY-444
Owner: Scrooge
...
Method can also synchronize an existing 'targetRoot' to match with the contents of 'properties' key-values.
// Donald sells car to Minnie
properties["Cars:0:Owner"] = "Minnie";
properties["Cars:0:PreviousOwners:2"] = "Donald";
// Scrooge's car is scrapped
properties.Remove("Cars:2:License");
properties.Remove("Cars:2:Owner");
// Synchronize modifications to the already existing 'root' node
root = YamlNodeExtensions.ToYaml(
properties: properties,
policy: YamlNodeExtensions.CorrelatePolicy.RemoveUnusedNodes | YamlNodeExtensions.CorrelatePolicy.RemoveAmbiguentNodes,
targetRoot: root,
delimiter: ':');
// Reprint document
stringWriter = new StringWriter();
yamlDocument = new YamlDocument(root);
yamlStream = new YamlStream(yamlDocument);
yamlStream.Save(stringWriter, assignAnchors: false);
text = stringWriter.ToString();
// Print
Console.WriteLine(text);
Cars:
- License: ABC-123
Owner: Minnie
PreviousOwners:
- Mickey
- Goofy
- Donald
- License: XYZ-456
Owner: Mickey
PreviousOwners:
- Pluto
...
CorrelatePolicy determines synchronization policy.
[Flags]
public enum CorrelatePolicy
{
RemoveUnusedNodes = 1,
RemoveAmbiguentNodes = 2,
}
Full Example
Full example
using Avalanche.Configuration;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;
using YamlDotNet.RepresentationModel;
public class yaml
{
public static void Run()
{
{
// <01>
// IConfigurationSource
var configurationSource = new YamlConfigurationProvider.Source
{
FileProvider = new PhysicalFileProvider(Directory.GetCurrentDirectory()),
Path = "appsettings.yaml",
ReloadOnChange = true,
Optional = true
};
// IConfigurationProvider
IConfigurationProvider configurationProvider = new YamlConfigurationProvider(configurationSource);
// Create configuration
IConfiguration configuration = new ConfigurationBuilder()
.Add(configurationSource)
.Build();
// </01>
Console.WriteLine(configuration.PrintTree());
}
{
// <11>
// Create configuration
IConfiguration configuration = new ConfigurationBuilder()
.AddYamlFile("appsettings.yaml", optional: true, reloadOnChange: true)
.Build();
// </11>
// Print configuration
Console.WriteLine(configuration.PrintTree());
}
{
// <12>
// Create host
IHost host =
new HostBuilder()
.ConfigureDefaults(null)
.ConfigureAppConfiguration(cb => cb.AddYamlFile("appsettings.yaml", optional: true, reloadOnChange: true))
.Build();
// Get configuration
IConfiguration configuration = host.Services.GetRequiredService<IConfiguration>();
// </12>
// Print configuration
Console.WriteLine(configuration.PrintTree());
}
{
// <21>
// Create properties
var properties = new Dictionary<string, string?>
{
{ "Cars:0:License", "ABC-123" },
{ "Cars:0:Owner", "Donald" },
{ "Cars:0:PreviousOwners:0", "Mickey" },
{ "Cars:0:PreviousOwners:1", "Goofy" },
{ "Cars:1:License", "XYZ-456" },
{ "Cars:1:Owner", "Mickey" },
{ "Cars:1:PreviousOwners:0", "Pluto" },
{ "Cars:2:License", "YYY-444" },
{ "Cars:2:Owner", "Scrooge" },
};
// Convert 'properties' to a YamlNode
YamlNode root = YamlNodeExtensions.ToYaml(
properties: properties,
policy: YamlNodeExtensions.CorrelatePolicy.RemoveUnusedNodes | YamlNodeExtensions.CorrelatePolicy.RemoveAmbiguentNodes,
targetRoot: null,
delimiter: ':');
// Convert to yaml document
var stringWriter = new StringWriter();
YamlDocument yamlDocument = new YamlDocument(root);
YamlStream yamlStream = new YamlStream(yamlDocument);
yamlStream.Save(stringWriter, assignAnchors: false);
string text = stringWriter.ToString();
// Print
Console.WriteLine(text);
// </21>
// <22>
// Donald sells car to Minnie
properties["Cars:0:Owner"] = "Minnie";
properties["Cars:0:PreviousOwners:2"] = "Donald";
// Scrooge's car is scrapped
properties.Remove("Cars:2:License");
properties.Remove("Cars:2:Owner");
// Synchronize modifications to the already existing 'root' node
root = YamlNodeExtensions.ToYaml(
properties: properties,
policy: YamlNodeExtensions.CorrelatePolicy.RemoveUnusedNodes | YamlNodeExtensions.CorrelatePolicy.RemoveAmbiguentNodes,
targetRoot: root,
delimiter: ':');
// Reprint document
stringWriter = new StringWriter();
yamlDocument = new YamlDocument(root);
yamlStream = new YamlStream(yamlDocument);
yamlStream.Save(stringWriter, assignAnchors: false);
text = stringWriter.ToString();
// Print
Console.WriteLine(text);
// </22>
}
}
}