Skip to content

Examples Overview

This section provides various usage examples for Apq.Cfg.

Default Levels

Each configuration source has a default level. If not specified, the default level is used:

Source TypeDefault Level
Json, Ini, Xml, Yaml, Toml0
Redis, Database100
Consul, Etcd, Nacos, Apollo, Zookeeper200
Vault300
.env, EnvironmentVariables400

Example Categories

Basic Examples

Integration Examples

Advanced Examples

Quick Examples

Simplest Usage

csharp
using Apq.Cfg;

var cfg = new CfgBuilder()
    .AddJsonFile("config.json")  // Uses default level 0
    .Build();

var appName = cfg["App:Name"];
Console.WriteLine($"App Name: {appName}");

Multi-Source

csharp
var cfg = new CfgBuilder()
    .AddJsonFile("config.json")                        // Uses default level 0
    .AddYamlFile("config.yaml", level: 10, optional: true)
    .AddEnvironmentVariables(prefix: "APP_")       // Uses default level 400
    .Build();

Strongly-Typed Binding

csharp
public class DatabaseConfig
{
    public string Host { get; set; } = "";
    public int Port { get; set; } = 5432;
}

var dbSection = cfg.GetSection("Database");
var dbConfig = new DatabaseConfig
{
    Host = dbSection["Host"] ?? "localhost",
    Port = dbSection.GetValue<int>("Port")
};
Console.WriteLine($"Database: {dbConfig.Host}:{dbConfig.Port}");

Dependency Injection

csharp
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddApqCfg(cfg => cfg
    .AddJsonFile("config.json")                        // Uses default level 0
    .AddEnvironmentVariables(prefix: "APP_"));    // Uses default level 400

builder.Services.ConfigureApqCfg<DatabaseConfig>("Database");

Dynamic Reload

csharp
var cfg = new CfgBuilder()
    .AddJsonFile("config.json", reloadOnChange: true)  // Uses default level 0
    .Build();

cfg.ConfigChanges.Subscribe(e =>
{
    Console.WriteLine("Configuration updated!");
    foreach (var (key, change) in e.Changes)
    {
        Console.WriteLine($"  [{change.Type}] {key}");
    }
});

Encryption & Masking

csharp
using Apq.Cfg.Crypto;

var cfg = new CfgBuilder()
    .AddJsonFile("config.json")            // Uses default level 0
    .AddAesGcmEncryptionFromEnv()      // Read key from environment variable
    .AddSensitiveMasking()             // Add masking support
    .Build();

// Encrypted value in config: { "Database": { "Password": "{ENC}base64..." } }
// Auto-decrypt on read
var password = cfg["Database:Password"];

// Use masked value for logging
Console.WriteLine($"Password: {cfg.GetMasked("Database:Password")}");
// Output: Password: myS***ord

Writable Configuration

csharp
var cfg = new CfgBuilder()
    .AddJsonFile("config.json", writeable: true, isPrimaryWriter: true)  // Uses default level 0
    .Build();

// Modify configuration
cfg.SetValue("App:Name", "NewName");
cfg.SetValue("Database:Port", "5433");

// Save to file
await cfg.SaveAsync();

Run Sample Project

The repository includes complete sample code:

bash
cd Samples/Apq.Cfg.Samples
dotnet run

Next Steps

Choose an example to learn more:

Released under the MIT License