Crip.Extensions.ConfigLocator 1.3.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Crip.Extensions.ConfigLocator --version 1.3.0
                    
NuGet\Install-Package Crip.Extensions.ConfigLocator -Version 1.3.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="Crip.Extensions.ConfigLocator" Version="1.3.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Crip.Extensions.ConfigLocator" Version="1.3.0" />
                    
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.3.0
                    
#r "nuget: Crip.Extensions.ConfigLocator, 1.3.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.
#:package Crip.Extensions.ConfigLocator@1.3.0
                    
#: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.3.0
                    
Install as a Cake Addin
#tool nuget:?package=Crip.Extensions.ConfigLocator&version=1.3.0
                    
Install as a Cake Tool

ConfigLocator

NuGet Version license

Crip.Extensions.ConfigLocator is a lightweight library for ASP.NET Core that automates the discovery and registration of configuration classes into the Dependency Injection (DI) container.

Tired of manually adding services.Configure<TOptions>(...) for every single options class? ConfigLocator handles it for you using simple attributes, keeping your Program.cs clean and your configuration organized.

🚀 Key Features

  • Auto-Discovery: Automatically scans assemblies for configuration classes.
  • Attribute-Based: Link classes to configuration sections directly in the class definition.
  • Validation Support: Built-in support for Data Annotations and custom IValidateOptions<T> validators.
  • Generic Attributes: Clean syntax for custom validators (C# 11+).
  • Multiple Types: Bind multiple types to the same configuration section effortlessly.
  • Lean & Fast: Optimized assembly scanning during startup.

🛠️ Getting Started

1. Installation

Install the package via NuGet:

dotnet add package Crip.Extensions.ConfigLocator

2. Setup

In your Program.cs, register the configuration locator. By default, it scans the calling assembly:

using Crip.Extensions.ConfigLocator;

var builder = WebApplication.CreateBuilder(args);

// Register all options from the calling assembly
builder.Services.AddConfigLocator(builder.Configuration);

// Or specify assemblies to scan
builder.Services.AddConfigLocator(builder.Configuration, typeof(MyOptions).Assembly);

📖 Usage

1. Decorate your Options class

Use the [ConfigLocation] attribute to specify the configuration section key.

using Crip.Extensions.ConfigLocator;

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

This class will automatically bind to the following in your appsettings.json:

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

2. Inject and Use

Inject these options anywhere using standard ASP.NET Core interfaces (IOptions<T>, IOptionsSnapshot<T>, or IOptionsMonitor<T>).

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

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

✅ Validation

The library seamlessly integrates with ASP.NET Core options validation.

Data Annotation Validation

Add the [ConfigValidate] attribute and use standard System.ComponentModel.DataAnnotations:

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

Custom Validators

For complex logic, provide a custom IValidateOptions<T> implementation:

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;
    }
}

// C# 11+ generic attribute syntax
[ConfigLocation("MySection")]
[ConfigValidate<MyOptionsValidator>] 
public class MyOptions
{
    public string ApiKey { get; set; } = null!;
}

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

🧩 Advanced Features

Multiple Types from Same Section

You can bind multiple types to the same configuration section using a single attribute:

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

⚠️ Limitations

  • Named Options: Currently not supported (uses Options.DefaultName).
  • Visibility: Scans for non-abstract classes. Supports public, internal, and nested classes.
  • Constructors: Requires a public parameterless constructor for binding (standard ASP.NET Core requirement).

🔗 Additional Resources

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.0 29 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