Path: redb-route/demo/SerialNumbersDemo/SerialNumbers.Worker/ContextConfiguration.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/demo/SerialNumbersDemo/SerialNumbers.Worker/ContextConfiguration.cs ---- using Microsoft.Extensions.Configuration; using redb.Route.Abstractions; namespace SerialNumbers.Worker; /// /// The context configuration the way the Tsak worker builds it, in miniature. Each layer deep-merges /// over the previous one, later layers win: /// /// Tsak:Contexts:default of the host configuration, /// Tsak:Contexts:{context}, /// the module's {Module}.config.json, /// Tsak:Contexts:{context}:Override, the operator's last word. /// /// Every root key then becomes a property of the route context; a section becomes a dictionary. The /// real worker also reads a module's context.json and per-context Contexts blocks inside /// the module files; this module uses neither. /// public static class ContextConfiguration { /// The context a module asks for, from its ContextName. public static string ContextNameOf(string moduleConfigPath) => new ConfigurationBuilder().AddJsonFile(moduleConfigPath, optional: false).Build()["ContextName"] ?? throw new InvalidOperationException($"{moduleConfigPath} has no ContextName."); public static IDictionary Build(IConfiguration host, string contextName, string moduleConfigPath) { var config = NewDictionary(); Merge(config, Read(host.GetSection("Tsak:Contexts:default"), skipReserved: true)); Merge(config, Read(host.GetSection($"Tsak:Contexts:{contextName}"), skipReserved: true)); Merge(config, Read(new ConfigurationBuilder().AddJsonFile(moduleConfigPath, optional: false).Build(), skipReserved: false)); Merge(config, Read(host.GetSection($"Tsak:Contexts:{contextName}:Override"), skipReserved: false)); return config; } public static void ApplyTo(IRouteContext context, IDictionary config) { foreach (var (key, value) in config) { if (value is not null) context.SetProperty(key, value); } } private static Dictionary Read(IConfiguration section, bool skipReserved) { var result = NewDictionary(); foreach (var child in section.GetChildren()) { // A context section carries the module list and the Override layer next to the settings. if (skipReserved && (child.Key.Equals("Modules", StringComparison.OrdinalIgnoreCase) || child.Key.Equals("Override", StringComparison.OrdinalIgnoreCase))) continue; result[child.Key] = child.GetChildren().Any() ? Read(child, skipReserved: false) : child.Value; } return result; } private static void Merge(IDictionary target, IDictionary layer) { foreach (var (key, value) in layer) { if (value is IDictionary nested && target.TryGetValue(key, out var existing) && existing is IDictionary current) Merge(current, nested); else target[key] = value; } } private static Dictionary NewDictionary() => new(StringComparer.OrdinalIgnoreCase); }