Mma.Configuration
9.0.3
dotnet add package Mma.Configuration --version 9.0.3
NuGet\Install-Package Mma.Configuration -Version 9.0.3
<PackageReference Include="Mma.Configuration" Version="9.0.3" />
<PackageVersion Include="Mma.Configuration" Version="9.0.3" />
<PackageReference Include="Mma.Configuration" />
paket add Mma.Configuration --version 9.0.3
#r "nuget: Mma.Configuration, 9.0.3"
#:package Mma.Configuration@9.0.3
#addin nuget:?package=Mma.Configuration&version=9.0.3
#tool nuget:?package=Mma.Configuration&version=9.0.3
Mma.Configuration
A powerful .NET configuration provider that loads application settings from SQL Server tables with JSON columns. This provider integrates seamlessly with the .NET configuration system and supports environment-specific configurations, automatic reloading, and complex JSON object storage.
🚀 Quick Start
Installation
dotnet add package Mma.Configuration --version 9.0.3
Or via NuGet Package Manager:
<PackageReference Include="Mma.Configuration" Version="9.0.3" />
Basic Usage
using Mma.Configuration.Providers.SqlServer;
var configuration = new ConfigurationBuilder()
.AddSqlServerJson(
connectionString: "Server=localhost;Database=MyApp;Integrated Security=true;TrustServerCertificate=true;")
.Build();
// Access configuration values
var dbConnectionString = configuration.GetConnectionString("Database");
var logLevel = configuration["Logging:LogLevel:Default"];
🏗️ Database Setup
Create your configuration table:
CREATE TABLE AppSettings (
Id int IDENTITY(1,1) PRIMARY KEY,
[Key] nvarchar(255) NOT NULL,
JsonValue nvarchar(max) NOT NULL,
Environment nvarchar(50) NULL,
CreatedAt datetime2 DEFAULT GETDATE(),
UpdatedAt datetime2 DEFAULT GETDATE(),
UNIQUE([Key], Environment)
);
✨ Key Features
- Environment-Specific Configuration: Support for development, staging, production environments with fallback
- JSON Object Storage: Store complex configuration objects as JSON with automatic flattening
- Automatic Reloading: Optional configuration reloading at specified intervals
- High Performance: Optimized SQL queries with connection pooling support
- Thread-Safe: Safe for use in multi-threaded applications
- Comprehensive Logging: Built-in logging for debugging and monitoring
- Flexible Schema: Customizable table and column names
- Error Resilience: Graceful error handling with retry mechanisms
🔧 Advanced Configuration
var configuration = new ConfigurationBuilder()
.AddSqlServerJson(source =>
{
source.ConnectionString = "Server=localhost;Database=MyApp;Integrated Security=true;";
source.TableName = "ApplicationSettings";
source.KeyColumn = "SettingKey";
source.JsonColumn = "SettingValue";
source.EnvironmentColumn = "Env";
source.Environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
source.ReloadOnChange = true;
source.ReloadInterval = TimeSpan.FromMinutes(10);
source.AutoCreateTable = false;
source.Optional = false;
source.MaxRetryAttempts = 5;
source.RetryDelay = TimeSpan.FromSeconds(30);
})
.Build();
📋 Configuration Options
| Property | Type | Default | Description |
|---|---|---|---|
ConnectionString |
string |
Required | SQL Server connection string |
TableName |
string |
"AppSettings" |
Name of the configuration table |
KeyColumn |
string |
"Key" |
Name of the key column |
JsonColumn |
string |
"JsonValue" |
Name of the JSON value column |
EnvironmentColumn |
string? |
null |
Name of the environment column (optional) |
Environment |
string? |
null |
Current environment name |
ReloadOnChange |
bool |
false |
Enable automatic reloading |
ReloadInterval |
TimeSpan |
TimeSpan.Zero |
Reload interval when ReloadOnChange is true |
AutoCreateTable |
bool |
false |
Automatically create table if it doesn't exist |
Optional |
bool |
false |
Whether the configuration source is optional |
MaxRetryAttempts |
int |
5 |
Maximum retry attempts on connection failure |
RetryDelay |
TimeSpan |
TimeSpan.FromSeconds(30) |
Delay between retry attempts |
🌍 ASP.NET Core Integration
var builder = WebApplication.CreateBuilder(args);
// Add SQL Server configuration early in the pipeline
builder.Configuration.AddSqlServerJson(
connectionString: builder.Configuration.GetConnectionString("ConfigDatabase")!,
environment: builder.Environment.EnvironmentName,
environmentColumn: "Environment",
reloadOnChange: true,
reloadInterval: TimeSpan.FromMinutes(5));
// Configure strongly-typed options
builder.Services.Configure<ApiSettings>(builder.Configuration.GetSection("ApiSettings"));
builder.Services.Configure<DatabaseOptions>(builder.Configuration.GetSection("Database"));
var app = builder.Build();
app.Run();
📊 JSON Flattening
The provider automatically flattens JSON objects into the standard .NET configuration format:
Input JSON:
{
"Database": {
"ConnectionString": "Server=localhost;Database=MyApp;",
"CommandTimeout": 30,
"Retry": {
"MaxAttempts": 3,
"DelaySeconds": 5
}
},
"Features": ["Feature1", "Feature2", "Feature3"]
}
Flattened Keys:
MySection:Database:ConnectionStringMySection:Database:CommandTimeoutMySection:Database:Retry:MaxAttemptsMySection:Database:Retry:DelaySecondsMySection:Features:0(Feature1)MySection:Features:1(Feature2)MySection:Features:2(Feature3)
🔄 Configuration Reloading
Enable automatic configuration reloading:
var configuration = new ConfigurationBuilder()
.AddSqlServerJson(
connectionString: connectionString,
reloadOnChange: true,
reloadInterval: TimeSpan.FromMinutes(5))
.Build();
// Register for change notifications
ChangeToken.OnChange(
() => configuration.GetReloadToken(),
() => {
Console.WriteLine("Configuration reloaded!");
// Handle configuration changes
});
🏃♂️ Performance Tips
- Use Connection Pooling: Include pooling settings in your connection string
- Create Indexes: Add appropriate indexes on your configuration table
- Optimize Reload Interval: Balance between freshness and performance
- Consider Caching: Implement additional caching for frequently accessed values
🛡️ Security Best Practices
- Store sensitive connection strings in secure stores (Azure Key Vault, etc.)
- Use principle of least privilege for database access
- Consider encrypting sensitive JSON values
- Validate and sanitize configuration inputs
📦 NuGet Package
This project is packaged as a NuGet package and outputs to the ./nupkg directory when built. The package includes:
- Main library assemblies
- Documentation files
- Package icon and metadata
- License information
Current Version: 9.0.3
Target Framework: .NET 9.0
Package Output: ./nupkg/Mma.Configuration.9.0.3.nupkg
🔧 Building
# Restore dependencies
dotnet restore
# Build the project
dotnet build
# Create NuGet package (automatically done on build)
dotnet pack
The NuGet package will be generated in the nupkg folder.
📚 Documentation
For comprehensive documentation, examples, and advanced usage scenarios, see the detailed documentation.
🐛 Troubleshooting
Common Issues:
- Connection String Issues: Verify server name, database name, and credentials
- JSON Parsing Errors: Validate JSON syntax and check for encoding issues
- Environment Configuration Not Loading: Verify environment names match database values
- Configuration Not Reloading: Ensure
ReloadOnChangeis true and connection is active
Debug Configuration:
// Enable detailed logging
builder.Logging.AddConsole();
builder.Logging.SetMinimumLevel(LogLevel.Debug);
// List all configuration values
foreach (var kvp in configuration.AsEnumerable())
{
Console.WriteLine($"{kvp.Key} = {kvp.Value}");
}
📄 License
This project is licensed under the MIT License - see the LICENSE.txt file for details.
🤝 Contributing
Contributions are welcome! Please feel free to submit issues, feature requests, or pull requests.
📞 Support
For support, please:
- Check the troubleshooting section
- Review the comprehensive documentation in the
docsfolder - Search existing issues on the project repository
- Create a new issue with detailed information
Project Repository: https://github.com/mma1979/mma-configuration
Package Manager: Available on NuGet
Framework: .NET 9.0
| Product | Versions 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. |
-
net9.0
- Microsoft.Data.SqlClient (>= 6.1.1)
- Microsoft.Extensions.Configuration (>= 9.0.8)
- Microsoft.Extensions.Configuration.Abstractions (>= 9.0.8)
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.8)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.