Crip.Extensions.ConfigLocator 1.4.2-rc.1

This is a prerelease version of Crip.Extensions.ConfigLocator.
There is a newer version of this package available.
See the version list below for details.
dotnet add package Crip.Extensions.ConfigLocator --version 1.4.2-rc.1
                    
NuGet\Install-Package Crip.Extensions.ConfigLocator -Version 1.4.2-rc.1
                    
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="Crip.Extensions.ConfigLocator" Version="1.4.2-rc.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Crip.Extensions.ConfigLocator" Version="1.4.2-rc.1" />
                    
Directory.Packages.props
<PackageReference Include="Crip.Extensions.ConfigLocator" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Crip.Extensions.ConfigLocator --version 1.4.2-rc.1
                    
#r "nuget: Crip.Extensions.ConfigLocator, 1.4.2-rc.1"
                    
#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.
#:package Crip.Extensions.ConfigLocator@1.4.2-rc.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Crip.Extensions.ConfigLocator&version=1.4.2-rc.1&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Crip.Extensions.ConfigLocator&version=1.4.2-rc.1&prerelease
                    
Install as a Cake Tool

ConfigLocator

NuGet Version license

Crip.Extensions.ConfigLocator helps ASP.NET Core apps register options classes automatically.

Instead of scattering AddOptions<T>().Bind(...) or services.Configure<TOptions>(...) across startup, mark your options with an attribute and let the library do the wiring.

What it does

  • Finds options classes in your assemblies
  • Binds them to configuration sections
  • Supports validation
  • Can bind more than one type from the same section

Quick start

Install the package:

dotnet add package Crip.Extensions.ConfigLocator

The package also ships a project skill to .github/skills/config-locator-code-review, which gives Copilot migration and review guidance for this library in consuming repositories.

Register it in Program.cs:

using Crip.Extensions.ConfigLocator;

var builder = WebApplication.CreateBuilder(args);

// Put config locator registration in the composition root, right after
// the builder is created and before services that consume options.
builder.Services.AddConfigLocator(builder.Configuration);

If your attributed options live outside the entry assembly, pass those assemblies explicitly:

builder.Services.AddConfigLocator(
    builder.Configuration,
    typeof(MyOptions).Assembly,
    typeof(SharedOptionsMarker).Assembly);

Define an options class

using Crip.Extensions.ConfigLocator;

[ConfigLocation("ExternalServices:GitHub")]
public class GitHubOptions
{
    public string ApiKey { get; set; } = string.Empty;
    public int TimeoutSeconds { get; set; } = 30;
}

This maps to appsettings.json:

{
  "ExternalServices": {
    "GitHub": {
      "ApiKey": "your-api-key",
      "TimeoutSeconds": 60
    }
  }
}

Use it like any other ASP.NET Core options class:

public class GitHubService(IOptions<GitHubOptions> options)
{
    private readonly GitHubOptions _options = options.Value;

    public void DoSomething() => Console.WriteLine(_options.ApiKey);
}

Copilot skill

The package includes a repository skill at .github/skills/config-locator-code-review/SKILL.md.

Its guidance is focused on:

  • where AddConfigLocator(...) should be initialized
  • how to migrate from AddOptions<T>().Bind(...) to ConfigLocationAttribute
  • how to review changes and steer owned options types away from manual binding

The packaged MSBuild target copies the skill to the consuming solution root when $(SolutionDir) is available, falls back to the repository root when a .git directory is found, and finally falls back to the project directory.

You can disable automatic installation in a consuming project by setting:

<PropertyGroup>
  <ConfigLocatorDisableSkillInstall>true</ConfigLocatorDisableSkillInstall>
</PropertyGroup>

Validation

You can use standard data annotations or a custom validator.

Data annotations

[ConfigLocation("MySection")]
[ConfigValidate]
public class MyOptions
{
    [Required, MinLength(5)]
    public string ApiKey { get; set; } = null!;
}

Custom validator

public class MyOptionsValidator : IValidateOptions<MyOptions>
{
    public ValidateOptionsResult Validate(string? name, MyOptions options)
    {
        if (options.ApiKey == "default")
            return ValidateOptionsResult.Fail("API Key cannot be 'default'");

        return ValidateOptionsResult.Success;
    }
}

[ConfigLocation("MySection")]
[ConfigValidate<MyOptionsValidator>]
public class MyOptions
{
    public string ApiKey { get; set; } = null!;
}

// For older C# versions, use:
// [ConfigValidate(typeof(MyOptionsValidator))]

This keeps validation on the options type instead of registering IValidateOptions<T> manually in Program.cs.


More than one type

You can bind multiple types to the same section:

[ConfigLocation("ServiceSettings", typeof(AdditionalOptions))]
public class MainOptions
{
    // ...
}

Named options

You can register the same options class more than once by giving each registration a name:

[ConfigLocation("GitHub:Default")]
[ConfigLocation("GitHub:Tenants:Europe", "europe")]
[ConfigLocation("GitHub:Tenants:Us", "us")]
public class GitHubOptions
{
    public string ApiKey { get; set; } = string.Empty;
}

Then resolve a specific named instance:

public class GitHubClient(IOptionsMonitor<GitHubOptions> options)
{
    private readonly GitHubOptions _europe = options.Get("europe");
    private readonly GitHubOptions _us = options.Get("us");
}

Migrating from manual binding

Prefer this migration path for owned options types:

  1. Move the section path to [ConfigLocation("...")] on the options class.
  2. Move validation to [ConfigValidate] or [ConfigValidate<TValidator>].
  3. Add builder.Services.AddConfigLocator(builder.Configuration); in startup.
  4. Remove manual AddOptions<T>(), .Bind(...), Configure<T>(...), and manual validator registration for that type.

Example migration:

// Before
builder.Services.AddOptions<PaymentsOptions>()
    .Bind(builder.Configuration.GetSection("Payments"))
    .ValidateDataAnnotations();

builder.Services.AddSingleton<IValidateOptions<PaymentsOptions>, PaymentsOptionsValidator>();
// After
[ConfigLocation("Payments")]
[ConfigValidate<PaymentsOptionsValidator>]
public class PaymentsOptions
{
    public string ApiKey { get; set; } = string.Empty;
}

builder.Services.AddConfigLocator(builder.Configuration);

For step-by-step migration and review guidance inside Copilot, use the shipped config-locator-code-review skill.


Limitations

  • Only non-abstract classes are scanned
  • Binding requires a public parameterless constructor

More info

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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.  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 is compatible.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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
1.4.2 0 8/25/2026
1.4.2-rc.2 0 8/25/2026
1.4.2-rc.1 0 8/25/2026
1.4.0 43 8/24/2026
1.3.0 91 8/16/2026
1.2.0 94 8/16/2026
1.1.0 367 7/5/2023
1.1.0-rc.1 202 7/5/2023
1.0.0 361 8/24/2022