ConfigurationValidation.AspNetCore 2.0.0

dotnet add package ConfigurationValidation.AspNetCore --version 2.0.0
NuGet\Install-Package ConfigurationValidation.AspNetCore -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="ConfigurationValidation.AspNetCore" Version="2.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ConfigurationValidation.AspNetCore --version 2.0.0
#r "nuget: ConfigurationValidation.AspNetCore, 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.
// Install ConfigurationValidation.AspNetCore as a Cake Addin
#addin nuget:?package=ConfigurationValidation.AspNetCore&version=2.0.0

// Install ConfigurationValidation.AspNetCore as a Cake Tool
#tool nuget:?package=ConfigurationValidation.AspNetCore&version=2.0.0

ConfigurationValidation.AspNetCore

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 shorcuts for configuration validation usage in Asp.Net application for easy functionality registrations.

Member of Salix.AspNetCore.Utilities packages

See also other packages for some other/related functionality in Asp.Net Core (mostly APIs):

ConfigurationValidation

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).

ASP.NET Integration

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).

Configuration object registration

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"))

Prevent startup (Filter)

// Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddConfigurationValidation();

Yellow error page

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();
// ...

Error page

Health check

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");
// ...
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0 394 11/19/2023
1.0.0 235 1/6/2023

Added multi-framework targetting and updated deprecated packages.