icfg-root.md 1.6 KB

ICfgRoot

The main configuration root interface.

Properties

ConfigChanges

Observable stream of configuration changes.

IObservable<ConfigChangeEvent> ConfigChanges { get; }

Methods

Get

Get a configuration value.

string? Get(string key)
T? Get<T>(string key)

Exists

Check if a key exists.

bool Exists(string key)

GetSection

Get a configuration section.

ICfgSection GetSection(string key)

Set

Set a configuration value.

void Set(string key, string? value, int? targetLevel = null)

Remove

Remove a configuration key.

void Remove(string key, int? targetLevel = null)

SaveAsync

Save pending changes.

Task SaveAsync(int? targetLevel = null, CancellationToken ct = default)

GetMany

Get multiple values.

IReadOnlyDictionary<string, string?> GetMany(IEnumerable<string> keys)
void GetMany(IEnumerable<string> keys, Action<string, string?> onValue)

SetMany

Set multiple values.

void SetMany(IEnumerable<KeyValuePair<string, string?>> values, int? targetLevel = null)

ToMicrosoftConfiguration

Convert to IConfigurationRoot.

IConfigurationRoot ToMicrosoftConfiguration()

Example

// Read
var name = cfg.Get("App:Name");
var port = cfg.Get<int>("App:Port");

// Write
cfg.Set("App:Name", "NewName");
await cfg.SaveAsync();

// Subscribe to changes
cfg.ConfigChanges.Subscribe(e => Console.WriteLine("Changed!"));

Next Steps