Please visit my MongoOptions.Blazor Github, it will be a usuable project to add Razor components to manage your Config files
A high-performance, resilient configuration provider for .NET 10 that uses MongoDB as a backing store with built-in memory caching and data validation. Update your config files without reloading you project.
- Fluent Configuration: Set up in seconds with a clean, readable API.
- Resilient Caching: Powered by
IMemoryCachewith "Stale-on-Failure" protection. - Keyed/Named Options: Support for multiple configuration instances (e.g., Tenant-specific settings).
- Data Validation: Built-in support for Data Annotations to keep your DB clean.
- Management API: Full CRUD support for managing configs via code (perfect for Blazor Admin UIs).
dotnet add package Tenowg.MongoOptionsUse standard Data Annotations for validation and our custom attribute for DB naming.
[MongoOption(DatabaseName = "AppSettings", CollectionName = "FeatureToggle")]
public partial class FeatureSettings
{
[Required]
public string Theme { get; set; } = "Light";
[Range(1, 100)]
public int MaxRetries { get; set; } = 5;
}
// this is currently optional, but if you want to use DataAnnotations for validation, it is required
// Source generation will fill in the rest of this class, you don't need to add anything else
[OptionsValidator]
public partial FeatureSettingsValidator : IValidateOption<FeatureSettings>
{}MongoOption Attribute is required for Source Generation, you don't need to assign a custom Database name or CollectionName.
Source Generation has been added MongoOptions.Generator. It is keyed to the attribute [MongoOptions] So if you want to make your life even easier, just tag you POCOs with [MongoOption] and add .RegisterDiscoveredOptions() to your DI Configurtion and all your Configurations will magically be added.
Use the Fluent API to hook everything up.
builder.Services.AddMongoConfiguration(config =>
{
config.ConnectionString = "mongodb://localhost:27017";
config.DatabaseName = "MyProductionApp";
})
.RegisterOptions<FeatureSettings>();
OR
.RegisterDiscoveredOptions();
.AddMongoWatch();AddMongoWatch(): Uses Change Stream from MongoDB to update configs on Database change, your database needs to be a replica set. This will be option, as there is also an internal mechanism to handle changes. This is mostly useful for Multi server syncing. This will be extendable, so look for Redis versions in the near future.
If you plan on using IOptionsMontor there is one more thing you need to add to your app.
app.RunMongoMonitor();It doesn't matter were you put it, but it needs to be there to clear the initial cache or monitor will not work. You don't need this if you are only going to use IOptionsSnapshot.
Inject IOptionsSnapshot<T> for automatic updates every request, or IOptionsMonitor<T> for real-time changes.
public class MyService(IOptionsSnapshot<FeatureSettings> settings)
{
public void DoWork()
{
// Access the "Default" config
var theme = settings.Value.Theme;
// Access a "Named" config
var tenantSettings = settings.Get("Tenant_A");
}
}This library ensures your app never runs with "garbage" data.
- Strict Lookups: If you request a named config that doesn't exist, it throws a
KeyNotFoundException. - Schema Protection: If a MongoDB document fails Data Annotation validation, an
OptionsValidationExceptionis thrown. - Database Downtime: If MongoDB goes offline, the library will continue to serve cached data from the cache for 1 minute before trying the DB again, preventing "request spam."
Use IConfigManager to build your own admin dashboard.
// Get all available config names for a type
var keys = await _configManager.GetKeys<FeatureSettings>();
// Save or Update
await _configManager.UpdateConfigAsync("NewTenant", mySettingsObject);
// Remove
await _configManager.RemoveConfig<FeatureSettings>("OldTenant");
// Clone
await _configManager.CloneConfigAsync<FeatureSettings>("SourceTenant", "TargetTenant");AddMongoConfiguration(Action<MongoConfigurationOptions>): Sets up MongoDB client and returns a builder.AddMongoOptions<T>(): Registers options for a type (alternative to builder).
RegisterOptions<T>(): Registers additional options types.
UpdateConfigAsync<T>(T): Updates default config.UpdateConfigAsync<T>(string, T): Updates named config.GetKeys<T>(): Lists all config names for a type.RemoveConfig<T>(string): Deletes a named config.CloneConfigAsync<T>(string, string): Copies a config to a new name.
ConnectionString: MongoDB connection string.DatabaseName: Default database name.CacheSoftDuration: Cache refresh interval.CacheHardDuration: Max cache lifetime.CachePrefix: Cache key prefix.
CollectionName: Custom collection name.DatabaseName: Custom database name.
This project uses .NET 10. You will need to pull Tenowg.MongoOptions.Generator and put it next to this project. There is a good chance you will need to edit the .csproj file to fix some file paths.
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
If you encounter any issues or have questions, please open an issue on GitHub.