Zircon.Configuration
2.0.0
dotnet add package Zircon.Configuration --version 2.0.0
NuGet\Install-Package Zircon.Configuration -Version 2.0.0
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="Zircon.Configuration" Version="2.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Zircon.Configuration" Version="2.0.0" />
<PackageReference Include="Zircon.Configuration" />
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 Zircon.Configuration --version 2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Zircon.Configuration, 2.0.0"
#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 Zircon.Configuration@2.0.0
#: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=Zircon.Configuration&version=2.0.0
#tool nuget:?package=Zircon.Configuration&version=2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Zircon.Configuration
Configuration extension methods providing enhanced functionality for Microsoft.Extensions.Configuration including required value retrieval with validation.
Features
- Required Value Retrieval: Get configuration values with automatic validation
- Clear Error Messages: Descriptive error messages showing the full configuration path
- Section Support: Works with both root configuration and configuration sections
- Exception Safety: Throws meaningful exceptions for missing configuration
Installation
dotnet add package Zircon.Configuration
Usage
Basic Configuration Access
using Microsoft.Extensions.Configuration;
using Zircon.Configuration;
// Configuration setup
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
// Get required values - throws if missing
string connectionString = configuration.GetRequiredValue("ConnectionStrings:DefaultConnection");
string apiKey = configuration.GetRequiredValue("ExternalApi:ApiKey");
Working with Configuration Sections
// Access nested configuration sections
var databaseSection = configuration.GetSection("Database");
string host = databaseSection.GetRequiredValue("Host");
string port = databaseSection.GetRequiredValue("Port");
// The error messages will show the full path: "Database:Host" or "Database:Port"
Dependency Injection Setup
public void ConfigureServices(IServiceCollection services)
{
var connectionString = Configuration.GetRequiredValue("ConnectionStrings:DefaultConnection");
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(connectionString));
var redisConnection = Configuration.GetRequiredValue("Redis:ConnectionString");
services.AddStackExchangeRedisCache(options =>
options.Configuration = redisConnection);
}
Configuration Validation
public class AppSettings
{
public string DatabaseConnectionString { get; set; }
public string JwtSecret { get; set; }
public string ExternalApiUrl { get; set; }
}
public void ValidateConfiguration(IConfiguration configuration)
{
// These will all throw with clear error messages if missing
var settings = new AppSettings
{
DatabaseConnectionString = configuration.GetRequiredValue("ConnectionStrings:Database"),
JwtSecret = configuration.GetRequiredValue("Jwt:Secret"),
ExternalApiUrl = configuration.GetRequiredValue("ExternalServices:ApiUrl")
};
}
Error Handling
When a required configuration value is missing, GetRequiredValue throws an InvalidOperationException with a descriptive message:
// If "Database:ConnectionString" is missing:
// InvalidOperationException: "Configuration missing value for: Database:ConnectionString"
// If "ApiKey" is missing from root configuration:
// InvalidOperationException: "Configuration missing value for: ApiKey"
Extension Method Details
GetRequiredValue
public static string GetRequiredValue(this IConfiguration configuration, string name)
Parameters:
configuration: TheIConfigurationinstance to retrieve the value fromname: The configuration key name
Returns:
string: The configuration value
Exceptions:
InvalidOperationException: Thrown when the configuration value is null or missing
Key Features:
- Automatically determines if the configuration is a section and includes the full path in error messages
- Works with both
IConfigurationandIConfigurationSectioninstances - Provides clear, actionable error messages for missing configuration
Comparison with Built-in Methods
| Method | Missing Value Behavior | Error Message Quality |
|---|---|---|
configuration["key"] |
Returns null |
No error - silent failure |
configuration.GetValue<string>("key") |
Returns null |
No error - silent failure |
configuration.GetRequiredValue("key") |
Throws exception | Clear error with full path |
Best Practices
- Use Early in Application Startup: Call
GetRequiredValueduring application configuration to fail fast - Validate Critical Settings: Use for database connections, API keys, and other essential configuration
- Combine with Options Pattern: Use alongside
IOptions<T>for structured configuration - Environment-Specific Validation: Ensure all required configuration is present across environments
Common Usage Patterns
Database Configuration
services.AddDbContext<ApplicationDbContext>(options =>
{
var connectionString = Configuration.GetRequiredValue("ConnectionStrings:DefaultConnection");
options.UseSqlServer(connectionString);
});
External Service Configuration
services.AddHttpClient("ExternalApi", client =>
{
var baseUrl = Configuration.GetRequiredValue("ExternalServices:BaseUrl");
var apiKey = Configuration.GetRequiredValue("ExternalServices:ApiKey");
client.BaseAddress = new Uri(baseUrl);
client.DefaultRequestHeaders.Add("X-API-Key", apiKey);
});
JWT Configuration
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
var secretKey = Configuration.GetRequiredValue("Jwt:SecretKey");
var issuer = Configuration.GetRequiredValue("Jwt:Issuer");
options.TokenValidationParameters = new TokenValidationParameters
{
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secretKey)),
ValidIssuer = issuer,
// ... other parameters
};
});
License
This project is licensed under the MIT License - see the LICENSE file for details.
| Product | Versions 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.
-
net10.0
- Microsoft.Extensions.Configuration.Abstractions (>= 10.0.2)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Zircon.Configuration:
| Package | Downloads |
|---|---|
|
Zircon.OpenApi.Swashbuckle
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.