Indiko.Blocks.Configuration.Consul 2.7.6

dotnet add package Indiko.Blocks.Configuration.Consul --version 2.7.6
                    
NuGet\Install-Package Indiko.Blocks.Configuration.Consul -Version 2.7.6
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Indiko.Blocks.Configuration.Consul" Version="2.7.6" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Indiko.Blocks.Configuration.Consul" Version="2.7.6" />
                    
Directory.Packages.props
<PackageReference Include="Indiko.Blocks.Configuration.Consul" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Indiko.Blocks.Configuration.Consul --version 2.7.6
                    
#r "nuget: Indiko.Blocks.Configuration.Consul, 2.7.6"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Indiko.Blocks.Configuration.Consul@2.7.6
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Indiko.Blocks.Configuration.Consul&version=2.7.6
                    
Install as a Cake Addin
#tool nuget:?package=Indiko.Blocks.Configuration.Consul&version=2.7.6
                    
Install as a Cake Tool

Indiko.Blocks.Configuration.Consul

Configuration block for loading application settings from HashiCorp Consul Key-Value store in distributed microservices environments.

Overview

This package provides a configuration block that loads settings from Consul KV store, enabling centralized configuration management for distributed applications. It supports dynamic configuration updates and service-specific configuration paths.

Features

  • Consul KV Integration: Load configuration from Consul Key-Value store
  • Service-Specific Config: Configuration paths scoped by service ID
  • JSON Configuration: Store configuration as JSON in Consul
  • Dynamic Updates: Support for configuration changes without restart
  • Early Loading: Loads before other blocks (BlockLoadOrder -1)
  • IConfiguration Integration: Seamlessly integrates with ASP.NET Core configuration
  • Fallback Support: Gracefully handles missing configuration keys

Installation

dotnet add package Indiko.Blocks.Configuration.Consul

Quick Start

Configuration (appsettings.json)

{
  "ConsulConfigurationStore": {
    "ServiceId": "my-service",
    "ConfigurationPath": "appsettings",
    "ConsulAddress": "http://consul:8500"
  }
}

Enable in Your Application

The Consul configuration block is typically auto-discovered and loaded. Ensure the ConsulConfigurationStore section is configured.

Store Configuration in Consul

Store your configuration as JSON in Consul under the key: {ServiceId}/{ConfigurationPath}

Example: my-service/appsettings

{
  "Database": {
    "ConnectionString": "Server=db-server;Database=mydb",
    "MaxRetryCount": 3
  },
  "ApiSettings": {
    "Endpoint": "https://api.example.com",
    "Timeout": 30
  },
  "FeatureFlags": {
    "EnableNewUI": true,
    "EnableBetaFeatures": false
  }
}

Using Consul CLI

# Write configuration to Consul
consul kv put my-service/appsettings @appsettings.json

# Read configuration
consul kv get my-service/appsettings

# Update a specific value
consul kv put my-service/appsettings '{"Database":{"ConnectionString":"..."}}'

Usage Examples

Accessing Configuration

using Microsoft.Extensions.Configuration;

public class MyService
{
    private readonly IConfiguration _configuration;

    public MyService(IConfiguration configuration)
    {
        _configuration = configuration;
    }

    public void Connect()
    {
        var connectionString = _configuration["Database:ConnectionString"];
        var maxRetries = _configuration.GetValue<int>("Database:MaxRetryCount");
    }
}

Type-Safe Configuration

// Define settings class
public class DatabaseSettings
{
    public string ConnectionString { get; set; }
    public int MaxRetryCount { get; set; }
}

public class ApiSettings
{
    public string Endpoint { get; set; }
    public int Timeout { get; set; }
}

// Register in ConfigureServices
services.Configure<DatabaseSettings>(Configuration.GetSection("Database"));
services.Configure<ApiSettings>(Configuration.GetSection("ApiSettings"));

// Inject and use
public class MyService
{
    private readonly DatabaseSettings _dbSettings;
    private readonly ApiSettings _apiSettings;

    public MyService(
        IOptions<DatabaseSettings> dbOptions,
        IOptions<ApiSettings> apiOptions)
    {
        _dbSettings = dbOptions.Value;
        _apiSettings = apiOptions.Value;
    }
}

Explicit Service Registration

using Indiko.Blocks.Configuration.Consul.Extensions;

public override void ConfigureServices(IServiceCollection services)
{
    // Explicitly use Consul configuration
    services.UseConsulConfiguration();
    
    base.ConfigureServices(services);
}

Key Components

ConsulSettingsConfigurationBlock

The main configuration block implementation.

[BlockLoadOrder(-1)]
public sealed class ConsulSettingsConfigurationBlock : BlockBase, IConsulSettingsConfigurationBlock
{
    public override void ConfigureServices(IServiceCollection services)
    {
        services.UseConsulConfiguration();
        base.ConfigureServices(services);
    }
}

ConsulConfigurationProvider

Provider that loads configuration from Consul KV store.

public sealed class ConsulConfigurationProvider : ConfigurationProvider
{
    public override void Load()
    {
        // Load from Consul: {ServiceId}/{ConfigurationPath}
    }
}

ConsulConfigurationSource

Configuration source for Consul integration.

public class ConsulConfigurationSource : IConfigurationSource
{
    public IConfigurationProvider Build(IConfigurationBuilder builder)
    {
        return new ConsulConfigurationProvider(serviceId, configurationPath, consulClient);
    }
}

Configuration Options

ConsulConfigurationStoreOptions

public class ConsulConfigurationStoreOptions
{
    // Service identifier used as base path in Consul KV
    public string ServiceId { get; set; }
    
    // Configuration file name/path (e.g., "appsettings")
    public string ConfigurationPath { get; set; }
    
    // Consul server address (e.g., "http://consul:8500")
    public string ConsulAddress { get; set; }
}

Full Configuration Example

{
  "ConsulConfigurationStore": {
    "ServiceId": "user-service",
    "ConfigurationPath": "appsettings",
    "ConsulAddress": "http://localhost:8500"
  }
}

Advanced Scenarios

Environment-Specific Configuration

Store different configurations per environment:

# Development
consul kv put user-service/appsettings.development @appsettings.dev.json

# Production
consul kv put user-service/appsettings.production @appsettings.prod.json

Configure the path based on environment:

{
  "ConsulConfigurationStore": {
    "ServiceId": "user-service",
    "ConfigurationPath": "appsettings.production",
    "ConsulAddress": "http://consul:8500"
  }
}

Multiple Configuration Paths

Load configuration from multiple Consul paths:

var builder = new ConfigurationBuilder();

// Load shared configuration
builder.AddConsul("shared/database", options =>
{
    options.ConsulAddress = "http://consul:8500";
});

// Load service-specific configuration
builder.AddConsul("user-service/appsettings", options =>
{
    options.ConsulAddress = "http://consul:8500";
});

var configuration = builder.Build();

Hierarchical Configuration

Organize configuration hierarchically in Consul:

shared/
  database
  cache
  logging
user-service/
  appsettings
  features
order-service/
  appsettings
  features

Configuration Watching and Reload

Monitor Consul for configuration changes:

public class MyService
{
    private readonly IOptionsMonitor<DatabaseSettings> _settings;

    public MyService(IOptionsMonitor<DatabaseSettings> settings)
    {
        _settings = settings;
        
        // React to configuration changes
        _settings.OnChange(newSettings =>
        {
            Console.WriteLine("Database configuration changed!");
            ReconfigureDatabase(newSettings);
        });
    }

    private void ReconfigureDatabase(DatabaseSettings settings)
    {
        // Update database connection pool, etc.
    }
}

Deployment Patterns

Docker Compose

version: '3.8'
services:
  consul:
    image: consul:latest
    ports:
      - "8500:8500"
    volumes:
      - consul-data:/consul/data

  my-service:
    build: .
    environment:
      - ConsulConfigurationStore__ConsulAddress=http://consul:8500
    depends_on:
      - consul

volumes:
  consul-data:

Kubernetes

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  appsettings.json: |
    {
      "ConsulConfigurationStore": {
        "ServiceId": "user-service",
        "ConfigurationPath": "appsettings",
        "ConsulAddress": "http://consul.default.svc.cluster.local:8500"
      }
    }

Benefits

  1. Centralized Management: Single source of truth for configuration
  2. Dynamic Updates: Change configuration without redeploying
  3. Service Discovery: Integrates with Consul service mesh
  4. Version Control: Consul KV supports versioning
  5. Access Control: Consul ACL for secure configuration
  6. Audit Trail: Track configuration changes

Error Handling

The provider gracefully handles missing keys:

// Returns null if key doesn't exist
var value = Configuration["NonExistent:Key"];

// Use GetValue with default
var timeout = Configuration.GetValue<int>("Api:Timeout", 30);

Target Framework

  • .NET 10

Dependencies

  • Indiko.Blocks.Configuration.Abstractions
  • Indiko.Blocks.Common.Abstractions
  • Consul
  • Microsoft.Extensions.Configuration

License

See LICENSE file in the repository root.

  • Indiko.Blocks.Configuration.Abstractions - Configuration abstractions
  • Indiko.Blocks.Configuration.AppSettings - AppSettings-based configuration
  • Indiko.Blocks.Communication.ServiceToService.Consul - Consul service discovery
  • Indiko.Blocks.Common.Management - Block management system

Resources

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.7.6 0 4/23/2026
2.7.5 0 4/23/2026
2.7.4 0 4/23/2026
2.7.3 0 4/23/2026
2.7.2 0 4/23/2026
2.7.1 3 4/23/2026
2.7.0 19 4/23/2026
2.6.4 84 4/21/2026
2.6.3 74 4/21/2026
2.6.2 82 4/21/2026
2.6.1 90 4/18/2026
2.6.0 83 4/17/2026
2.5.1 89 4/14/2026
2.5.0 108 3/30/2026
2.2.18 95 3/8/2026
2.2.17 84 3/8/2026
2.2.16 88 3/8/2026
2.2.15 90 3/7/2026
2.2.13 85 3/7/2026
2.2.12 88 3/7/2026
Loading failed