ConfigurationOptionsValidator 1.0.0
Version 1.0.0: The initial release of the ConfigurationOptionsValidator. Documentation was not included in this version.
See the version list below for details.
dotnet add package ConfigurationOptionsValidator --version 1.0.0
NuGet\Install-Package ConfigurationOptionsValidator -Version 1.0.0
<PackageReference Include="ConfigurationOptionsValidator" Version="1.0.0" />
<PackageVersion Include="ConfigurationOptionsValidator" Version="1.0.0" />
<PackageReference Include="ConfigurationOptionsValidator" />
paket add ConfigurationOptionsValidator --version 1.0.0
#r "nuget: ConfigurationOptionsValidator, 1.0.0"
#:package ConfigurationOptionsValidator@1.0.0
#addin nuget:?package=ConfigurationOptionsValidator&version=1.0.0
#tool nuget:?package=ConfigurationOptionsValidator&version=1.0.0
ConfigurationOptionsValidator
ConfigurationOptionsValidator is a powerful library for .NET that simplifies the process of validating configuration settings injected into your ASP.NET Core application using the Options pattern. This library ensures that your appsettings.json configurations are correctly set and that all required settings are present and valid.
ConfigurationOptionsValidator targets .NET 8 and ensures compatibility with the latest features and improvements in the .NET ecosystem.
With ConfigurationOptionsValidator, developers can validate their configurations easily, ensuring the application runs smoothly with the correct configuration values.
Note: The solution uses Serilog for logging configuration validation errors. Logs are stored in the specified logPath
. If the logPath
is not provided, a log file will be created by default in C:\TaskLogs
.
ConfigurationOptionsValidator works seamlessly with the Options Pattern, which is a recommended approach for managing configuration in .NET. The Options pattern removes magic strings from your code, making it more readable and flexible. While it�s a great practice to use, even if you�re not using the Options pattern, this library can still help by validating your appsettings values and checking for any missing or incorrect properties.
Features
- Validates configuration settings injected using the Options pattern.
- Ensures all required configurations are present and correctly set in
appsettings.json
. - Provides a simple and effective way to validate configuration types during application startup.
- Supports validation for both Blazor and API projects.
- Includes security features to prevent exposing error details to external users.
Installation
Install the package via NuGet:
dotnet add package ConfigurationOptionsValidator --version 1.0.0
Setup
1. Register Services
In your Program.cs
file, add the following to register the ConfigurationValidationManager
service:
builder.Services.AddSingleton<ConfigurationValidationManager>();
Then, register the configuration settings by binding them to classes in your appsettings.json
:
builder.Services.Configure<YourConfigOptions>(builder.Configuration.GetSection(nameof(YourConfigOptions)));
Usage Examples
2. Validate Configurations in Blazor
To validate your configurations in a Blazor application, retrieve the ConfigurationValidationManager
from dependency injection and call the ValidateConfigurationTypes
method:
var configValidator = app.Services.GetRequiredService<ConfigurationValidationManager>();
configValidator.ValidateConfigurationTypes(
typeof(YourFirstConfigOptions),
typeof(YourSecondConfigOptions),
typeof(YourThirdConfigOptions)
);
);
var serilogOptions = app.Services.GetRequiredService<IOptions<SerilogOptions>>().Value;
app.MapBlazorConfigError( configValidator.MissingProperties,serilogOptions.WriteTo.FirstOrDefault()?.Args.Path);
3. Validate Configurations in API
In your API project, validate configurations by calling the ValidateApiConfiguration
method:
if(!builder.ValidateApiConfiguration(logPath: logPath,
typeof(YourFirstConfigOptions),
typeof(YourSecondConfigOptions),
typeof(YourThirdConfigOptions)
{
Environment.Exit(1);
}
Error Handling and Security
For enhanced security, configure error handling to restrict access to error pages in production environments. You can add the following settings to your appsettings.json
to prevent exposing errors to end users:
"ConfigurationErrorPageSettings": {
"ForbiddenMessage": "Access to this page is restricted to authorized users only.",
"SupportContact": {
"Email": "support@company.com",
"Phone": "+44 7890 123456",
"Website": "https://support.company.com"
}
}
This ensures that error messages are not displayed to the public, but are only available to the server hosting the application.
Why Use ConfigurationOptionsValidator?
With ConfigurationOptionsValidator, you no longer need to:
- Manually check each configuration setting during application startup.
- Expose sensitive configuration errors to end users.
ConfigurationOptionsValidator saves developers time and ensures a smoother, more reliable setup process for your ASP.NET Core applications.
Contributing
We welcome contributions! Please feel free to:
- Submit pull requests.
- Report issues or request features in the GitHub repository.
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 | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. 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. |
-
net8.0
- Microsoft.Extensions.Options (>= 8.0.2)
- Serilog (>= 4.2.0)
- Serilog.Sinks.File (>= 6.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.