ConfigurationProcessor.Core
0.9.3
See the version list below for details.
dotnet add package ConfigurationProcessor.Core --version 0.9.3
NuGet\Install-Package ConfigurationProcessor.Core -Version 0.9.3
<PackageReference Include="ConfigurationProcessor.Core" Version="0.9.3" />
paket add ConfigurationProcessor.Core --version 0.9.3
#r "nuget: ConfigurationProcessor.Core, 0.9.3"
// Install ConfigurationProcessor.Core as a Cake Addin #addin nuget:?package=ConfigurationProcessor.Core&version=0.9.3 // Install ConfigurationProcessor.Core as a Cake Tool #tool nuget:?package=ConfigurationProcessor.Core&version=0.9.3
ConfigurationProcessor.DependencyInjection
This library registers and configures services in a service collection using .NET's' configuration library.
Example
Given an application with a ConfigureServices
section like below:
services.AddLogging();
services.AddHsts(options =>
{
options.Preload = true;
options.IncludeSubDomains = true;
options.MaxAge = TimeSpan.FromDays(365);
});
services.Configure<CookiePolicyOptions>(options =>
{
options.HttpOnly = HttpOnlyPolicy.Always;
options.Secure = CookieSecurePolicy.Always;
});
The ConfigureServices
method above can be moved to the configuration using the code and the appsettings.config configuration as in below:
services.AddFromConfiguration(Configuration, "Services");
{
"Services": {
"Logging": true,
"Hsts": {
"Preload": true,
"IncludeSubDomains": true,
"MaxAge": "356.00:00:00"
},
"Configure<Microsoft.AspNetCore.Builder.CookiePolicyOptions>": {
"HttpOnly": "Always",
"Secure": "Always"
}
}
}
Since we are using IConfiguration
, we aren't limited to the appsettings.json for configuring our services. We can also have the configuration in environment variables, in command-line arguments or with custom configuration providers such as AWS Secrets Manager.
Basics
The library works by using reflection and scanning all currently loaded assemblies for extension methods for IServiceCollection
. This project was inspied by the Serilog.Settings.Configuration
project.
Extension method mapping and overload resolution
Given a configuration named Logging
, an extension method named AddLogging
or Logging
will be filtered from the candidate extension methods. If multiple candidates are found, the best overload will be chosen based on the name of the input parameters.
Given the following extension methods:
public IServiceCollection AddMyService(this IServiceCollection services);
public IServiceCollection AddMyService(this IServiceCollection services, string name);
public IServiceCollection AddMyService(this IServiceCollection services, int count);
When given the configuration below, the extension method AddMyService(IServiceCollection, int)
is chosen.
{
"Services": {
"MyService" : {
"Count": 23
}
}
}
If the extension method is parameterless, use true
instead of an object. The configuration method below will choose AddMyService(IServiceCollection)
. A false
value will prevent registration.
{
"Services": {
"MyService" : true
}
}
Action Delegate mapping
ConfigurationProcessor can be used with extension methods that use an action delegate.
Given the extension method below:
public IServiceCollection AddMyService(this IServiceCollection services, Action<MyServiceOptions> configureOptions);
public class MyServiceOptions
{
public string Title { get; set; }
}
The configuration below is equivalent to calling services.AddMyService(options => {});
:
{
"MyService" : true
}
The configuration below is equivalent to calling services.AddMyService(options => { options.Title = "abc" });
:
{
"MyService" : {
"Title": "abc"
}
}
Generic extension method mapping
Generic extension methods can be mapped by supplying the generic parameter via the angle brackets <>
. The full name of the type must be supplied.
public IServiceCollection AddMyService<T>(this IServiceCollection services, T value);
{
"MyService<System.String>" : {
"Value": "hello world"
}
}
Mapping to extension methods with a single array parameter
Extension methods that have a single array parameter can be mapped with json arrays. This will work with or without the params
keyword. This can also work with a List<>
parameter type.
public IServiceCollection AddMyService(this IServiceCollection services, params string[] values);
{
"MyService" : [
"salut",
"hi",
"konichiwa"
]
}
Mapping to extension methods with a single dictionary parameter
Extension methods that have a single dictionary parameter can be mapped with json objects. Currently, only the parameter type Dictionary<,>
is supported. If one of the property names matches with a parameter name in an extension method overload, the overload with the matching parameter name is chosen instead of the dictionary overload.
public IServiceCollection AddMyService(this IServiceCollection services, Dictionary<string, int> values);
{
"MyService" : {
"Value1": 1,
"Value2": 2
}
}
Supported string mappings
Some .NET types can be mapped from a string in configuration. These additional mappings will not work in some binding scenarios.
.NET Type | Example Configuration | C# Equivalent |
---|---|---|
System.Uri |
"http://github.com" |
new Uri("http://github.com") |
System.TimeSpan |
"00:00:30" |
TimeSpan.Parse("00:00:30", CultureInfo.InvariantCulture) |
System.Type |
"System.String" |
Type.GetType("System.String") * |
System.Reflection.Assembly |
"MyCorp.MyLib" |
Assembly.Load("MyCorp.MyLib") * |
*For Type
and Assembly
, the loaded types and assemblies are searched first before resorting to Type.GetType
or Assembly.Load
.
Delegate mapping and static members
A parameter/property type that is a delegate can be mapped to a static method.
namespace MyProject
{
public static class Helpers
{
public IServiceCollection AddMyService(
this IServiceCollection services,
Action<MyConfiguration> configureAction);
public static void LogOnError(ILogger instance, MyConfiguration configuration);
}
public class MyConfiguration
{
public Action<ILogger, MyConfiguration> OnError { get; set; }
}
}
The configuration below maps to services.AddMyService(config => config.OnError = Helpers.LogOnError)
{
"MyService" : {
"OnError": "MyProject.Helpers::LogOnError"
}
}
Product | Versions 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 was computed. 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. |
.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. |
-
.NETStandard 2.0
- Microsoft.Extensions.Configuration.Binder (>= 6.0.0)
- Microsoft.Extensions.DependencyModel (>= 6.0.0)
- System.Reflection.Emit (>= 4.7.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on ConfigurationProcessor.Core:
Package | Downloads |
---|---|
ConfigurationProcessor.DependencyInjection
This library registers and configures services in a service collection using an IConfiguration object. Built from 51249be0499b22a5287c262f5a1bf9e0a0f9d133 |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.12.0 | 302 | 9/3/2023 |
1.11.0 | 462 | 3/25/2023 |
1.10.1 | 726 | 11/14/2022 |
1.9.0 | 612 | 11/12/2022 |
1.8.2 | 611 | 11/12/2022 |
1.7.2 | 685 | 11/4/2022 |
1.7.1 | 718 | 10/25/2022 |
1.6.1 | 692 | 10/24/2022 |
1.6.0 | 710 | 10/23/2022 |
1.5.1 | 709 | 10/22/2022 |
1.5.0 | 707 | 10/22/2022 |
1.4.0 | 1,048 | 10/11/2022 |
1.3.0 | 789 | 10/7/2022 |
1.2.1 | 757 | 10/6/2022 |
1.2.0 | 845 | 9/19/2022 |
1.1.1 | 800 | 9/16/2022 |
1.1.0 | 854 | 9/2/2022 |
1.0.0 | 1,202 | 7/7/2022 |
0.9.9 | 804 | 7/5/2022 |
0.9.8 | 783 | 7/5/2022 |
0.9.7 | 776 | 7/4/2022 |
0.9.5 | 800 | 7/3/2022 |
0.9.4 | 812 | 7/3/2022 |
0.9.3 | 804 | 7/3/2022 |
0.9.2 | 784 | 7/3/2022 |
0.9.1 | 791 | 7/2/2022 |