Incorporates usage of ConfigurationValidation within Asp.Net (Core) applications.
Provides three ways of handling invalid configurations:
- Request filter preventing application startup on invalid configuration
- Root page (yellow page of error) displaying incorrect configuration values
- Health check for configuration
Also provides extension shortcuts for configuration validation usage in Asp.Net application for easy functionality registrations.
Consider "star" this project and/or better
See also other packages for some other/related functionality in Asp.Net Core (mostly APIs):
- JSON Error handler
- API dynamic FrontPage (with binaries versioning approaches)
- Health check with JSON result + Health page
Main functionality, which is wrapped here has its own repository (Click HERE) explaining Configuration validation approach. It has documentation describing how to create validatable configuration objects in application(s).
All usage is set up in program.cs
file.
See also repository Sample project on real implementation.
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// <----- Register validatable configuration(s) you define in your app (see ConfigurationValidation package on how to create these).
builder.Services.ConfigureValidatableSetting<SampleLogicConfig>(builder.Configuration.GetSection("LogicConfiguration"));
// <----- Enabling this will prevent app startup when config is wrong (should see startup logging for troubleshooting)
// builder.Services.AddConfigurationValidation();
// <----- Add Health check for configuration (also enable app.UseHeathChecks() below)
// builder.Services.AddConfigurationHealthCheck(builder.Environment.IsDevelopment());
builder.Services.AddControllers();
var app = builder.Build();
app.UseHttpsRedirection();
// <----- Displays yellow screen of error when config is wrong (only for root URL ("/")
app.UseConfigurationValidationErrorPage();
// <----- Enable together with builder.Services.AddConfgurationHealthCheck() above.
// app.UseHealthChecks("/health");
app.UseAuthorization();
app.MapControllers();
app.Run();
}
}
Code above displays all three approaches (two of them commented out).
Package has extension methods to register defined validatable configuration objects with dependency injection container.
They register both object itself and IOptions<> instances with container.
// Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.ConfigureValidatableSetting<MyConfigClass>(builder.Configuration.GetSection("MyConfigSection"));
// Register non-validatable configuration settings class
builder.Services.ConfigureSetting<MyConfigClass>(builder.Configuration.GetSection("MyConfigSection"))
// Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddConfigurationValidation();
Is shown when root page is invoked (https://myaspnetapp.com/) when configuration is invalidated. If configuration is correct - displays normal root page (if one exists). Does not show page or impact any sub-routes work (if they still work with incorrect configuration).
// Program.cs
var builder = WebApplication.CreateBuilder(args);
// ...
var app = builder.Build();
app.UseConfigurationValidationErrorPage();
// ...
Adds health check for validatable configuration objects.
NOTE: Someone should invoke health check to actually see configuration is broken (or use monitoring tools to get notified on that).
// Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddConfigurationHealthCheck(builder.Environment.IsDevelopment());
// ...
var app = builder.Build();
app.UseHealthChecks("/health");
// ...