CG.Infrastructure.Configuration 3.10.1

dotnet add package CG.Infrastructure.Configuration --version 3.10.1
                    
NuGet\Install-Package CG.Infrastructure.Configuration -Version 3.10.1
                    
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="CG.Infrastructure.Configuration" Version="3.10.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CG.Infrastructure.Configuration" Version="3.10.1" />
                    
Directory.Packages.props
<PackageReference Include="CG.Infrastructure.Configuration" />
                    
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 CG.Infrastructure.Configuration --version 3.10.1
                    
#r "nuget: CG.Infrastructure.Configuration, 3.10.1"
                    
#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 CG.Infrastructure.Configuration@3.10.1
                    
#: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=CG.Infrastructure.Configuration&version=3.10.1
                    
Install as a Cake Addin
#tool nuget:?package=CG.Infrastructure.Configuration&version=3.10.1
                    
Install as a Cake Tool

CG.Infrastructure.Configuration

NuGet .NET

A configuration management library that provides standardized options classes and service registration extensions for managing application configuration in .NET applications.

Overview

CG.Infrastructure.Configuration is a specialized library that provides a consistent approach to configuration management across the infrastructure suite. It offers predefined options classes for common configuration scenarios and extension methods for seamless integration with the .NET dependency injection system.

Features

  • Standardized Options Classes: Predefined configuration options for common scenarios
  • Connection Management: Flexible connection string and connection name configuration
  • Issuer Configuration: Environment-aware API client configuration for identity, authorization, and authentication services
  • Docker Environment Support: Automatic environment detection and configuration switching
  • Configuration Binding: Automatic binding of configuration sections to strongly-typed options
  • Dependency Injection Integration: Seamless integration with the DI container
  • Service Extensions: Extension methods for easy service registration

Requirements

  • .NET 9.0 or later
  • Microsoft.Extensions.Configuration.Abstractions 9.0.8+
  • Microsoft.Extensions.DependencyInjection.Abstractions 9.0.8+
  • Microsoft.Extensions.Options.ConfigurationExtensions 9.0.8+

Installation

dotnet add package CG.Infrastructure.Configuration

Or add the following to your .csproj file:

<PackageReference Include="CG.Infrastructure.Configuration" Version="3.10.0" />

Quick Start

1. Configure Application Settings

Add configuration to your appsettings.json:

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=localhost;Database=MyApp;Trusted_Connection=true;",
    "ReadOnlyConnection": "Server=localhost;Database=MyAppReadOnly;Trusted_Connection=true;"
  },
  "ConnectionOptions": {
    "ConnectionName": "Default"
  },
  "IssuerOptions": {
    "UseDocker": false,
    "IdentityApiClients": [
      "https://identity-api.docker.local",
      "https://identity-api.local"
    ],
    "AuthorizationApiClients": [
      "https://auth-api.docker.local",
      "https://auth-api.local"
    ],
    "AuthenticationApiClients": [
      "https://authn-api.docker.local",
      "https://authn-api.local"
    ]
  }
}

2. Register Configuration Services

In your Program.cs or Startup.cs:

using Infrastructure.Configuration.Extensions;
using Microsoft.Extensions.DependencyInjection;

var builder = WebApplication.CreateBuilder(args);

// Register configuration services with connection name
builder.Services.RegisterInfraConfigurationServices(builder.Configuration, "Default");

var app = builder.Build();

3. Use Configuration Options

[ApiController]
[Route("[controller]")]
public class ConfigurationController : ControllerBase
{
    private readonly ConnectionOptions _connectionOptions;
    private readonly IssuerOptions _issuerOptions;

    public ConfigurationController(
        ConnectionOptions connectionOptions,
        IssuerOptions issuerOptions)
    {
        _connectionOptions = connectionOptions;
        _issuerOptions = issuerOptions;
    }

    [HttpGet("connections")]
    public ActionResult<Dictionary<string, string>> GetConnections()
    {
        return Ok(_connectionOptions.Connections);
    }

    [HttpGet("identity-api")]
    public ActionResult<string> GetIdentityApiUrl()
    {
        return Ok(_issuerOptions.IdentityApiClient ?? "Not configured");
    }
}

Core Functionality

Connection Management

The library provides flexible connection string management through ConnectionOptions:

// Access connection strings
var connectionOptions = serviceProvider.GetRequiredService<ConnectionOptions>();

// Get all connections
var allConnections = connectionOptions.Connections;

// Get specific connection
var defaultConnection = connectionOptions.Connections["DefaultConnection"];

// Get current connection name
var connectionName = connectionOptions.ConnectionName;

Issuer Configuration

The IssuerOptions class provides environment-aware API client configuration:

// Get issuer options
var issuerOptions = serviceProvider.GetRequiredService<IssuerOptions>();

// Check if running in Docker
var isDocker = issuerOptions.UseDocker;

// Get appropriate API client URLs based on environment
var identityApiUrl = issuerOptions.IdentityApiClient;
var authorizationApiUrl = issuerOptions.AuthorizationApiClient;
var authenticationApiUrl = issuerOptions.AuthenticationApiClient;

Environment-Aware Configuration

The library automatically selects the appropriate configuration based on the environment:

// Configuration automatically switches based on UseDocker flag
if (issuerOptions.UseDocker)
{
    // Uses first URL in arrays (Docker environment)
    var apiUrl = issuerOptions.IdentityApiClients[0];
}
else
{
    // Uses second URL in arrays (Local environment)
    var apiUrl = issuerOptions.IdentityApiClients[1];
}

Configuration Options

ConnectionOptions

Property Description Default
Connections Dictionary of connection strings Empty dictionary
ConnectionName Name of the current connection "Default"

IssuerOptions

Property Description Default
UseDocker Flag indicating Docker environment false
IdentityApiClients Array of identity API URLs null
AuthorizationApiClients Array of authorization API URLs null
AuthenticationApiClients Array of authentication API URLs null

Computed Properties:

  • IdentityApiClient: Automatically selects appropriate URL based on environment
  • AuthorizationApiClient: Automatically selects appropriate URL based on environment
  • AuthenticationApiClient: Automatically selects appropriate URL based on environment

Service Registration

The library provides extension methods for easy service registration:

RegisterInfraConfigurationServices

Registers all configuration services with a specified connection name:

services.RegisterInfraConfigurationServices(configuration, "Production");

This method:

  1. Registers ConnectionOptions with the specified connection name
  2. Registers IssuerOptions with configuration binding
  3. Automatically binds configuration sections to options classes
  4. Registers options as singletons in the DI container

Manual Registration

You can also register individual options manually:

// Register connection options
services.RegisterConnectionOptions(configuration, "CustomConnection", options =>
{
    options.ConnectionName = "Custom";
});

// Register issuer options
services.RegisterIssuerOptions(configuration, options =>
{
    options.UseDocker = true;
});

Configuration Structure

appsettings.json Example

{
  "ConnectionStrings": {
    "Primary": "Server=primary-server;Database=PrimaryDB;",
    "Secondary": "Server=secondary-server;Database=SecondaryDB;",
    "ReadOnly": "Server=readonly-server;Database=ReadOnlyDB;"
  },
  "ConnectionOptions": {
    "ConnectionName": "Primary"
  },
  "IssuerOptions": {
    "UseDocker": false,
    "IdentityApiClients": [
      "https://identity.docker.local:5001",
      "https://identity.local:5001"
    ],
    "AuthorizationApiClients": [
      "https://auth.docker.local:5002",
      "https://auth.local:5002"
    ],
    "AuthenticationApiClients": [
      "https://authn.docker.local:5003",
      "https://authn.local:5003"
    ]
  }
}

Environment-Specific Configuration

// appsettings.Development.json
{
  "IssuerOptions": {
    "UseDocker": false
  }
}

// appsettings.Production.json
{
  "IssuerOptions": {
    "UseDocker": true
  }
}

Advanced Usage

Custom Configuration Binding

services.RegisterConnectionOptions(configuration, "Custom", options =>
{
    // Customize connection options
    options.ConnectionName = "CustomConnection";
    
    // Add additional connections programmatically
    options.Connections["CustomConnection"] = "Custom connection string";
});

Multiple Connection Profiles

// Register multiple connection profiles
services.RegisterConnectionOptions(configuration, "Primary");
services.RegisterConnectionOptions(configuration, "Secondary");

// Use in services
public class DataService
{
    private readonly ConnectionOptions _primaryConnections;
    private readonly ConnectionOptions _secondaryConnections;

    public DataService(
        [FromKeyedServices("Primary")] ConnectionOptions primaryConnections,
        [FromKeyedServices("Secondary")] ConnectionOptions secondaryConnections)
    {
        _primaryConnections = primaryConnections;
        _secondaryConnections = secondaryConnections;
    }
}

Configuration Validation

services.RegisterIssuerOptions(configuration, options =>
{
    // Validate configuration
    if (options.IdentityApiClients == null || options.IdentityApiClients.Length < 2)
    {
        throw new InvalidOperationException("IdentityApiClients must have at least 2 URLs");
    }
});

Dependencies

  • Microsoft.Extensions.Configuration.Abstractions: Configuration system integration
  • Microsoft.Extensions.DependencyInjection.Abstractions: Dependency injection support
  • Microsoft.Extensions.Options.ConfigurationExtensions: Configuration binding support

Integration with Infrastructure Suite

This library integrates with the broader infrastructure suite by:

  1. Standardized Configuration: Provides consistent configuration patterns across all infrastructure libraries
  2. Environment Awareness: Automatically adapts configuration based on deployment environment
  3. Service Registration: Integrates with the DI container for easy consumption
  4. Options Pattern: Follows Microsoft's recommended options pattern for configuration

Best Practices

  1. Environment Detection: Use the UseDocker flag to automatically switch between environments
  2. Connection Naming: Use descriptive connection names for different database contexts
  3. Configuration Validation: Validate critical configuration in the registration methods
  4. Singleton Registration: Options are registered as singletons for performance
  5. Configuration Binding: Leverage automatic configuration binding for type safety

Contributing

This library is part of the CG Infrastructure suite. For contributions, please follow the established patterns and ensure all tests pass.

License

This project is licensed under the terms specified in the LICENSE file.

Version History

  • 3.10.0: Current stable release
  • Supports .NET 9.0
  • Includes connection management and issuer configuration
  • Provides environment-aware configuration switching
  • Integrates with the infrastructure service registration system

Support

For issues, questions, or contributions, please refer to the project repository or contact the maintainers.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  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 (5)

Showing the top 5 NuGet packages that depend on CG.Infrastructure.Configuration:

Package Downloads
CG.Infrastructure.Data

Infra Data library with dbup and dapper

CG.Infrastructure.Http

Infra Http library with shared services

CG.Infrastructure.Health

Infra Health library with health checks

CG.Infrastructure.Authentication

Infra Authentication library with AspNetCore.Identity setup, extensions and database contexts

CG.Infrastructure.Authorization

Infra Authorization library with Duende setup, extensions and database contexts

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.10.1 203 8/10/2025
3.10.0 254 6/3/2025
3.9.2 168 2/20/2025
3.9.1 121 2/18/2025
3.9.0 205 12/10/2024
3.0.0 203 8/13/2024