ConfigurationProcessor.Core 0.9.8

There is a newer version of this package available.
See the version list below for details.
dotnet add package ConfigurationProcessor.Core --version 0.9.8                
NuGet\Install-Package ConfigurationProcessor.Core -Version 0.9.8                
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="ConfigurationProcessor.Core" Version="0.9.8" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ConfigurationProcessor.Core --version 0.9.8                
#r "nuget: ConfigurationProcessor.Core, 0.9.8"                
#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 ConfigurationProcessor.Core as a Cake Addin
#addin nuget:?package=ConfigurationProcessor.Core&version=0.9.8

// Install ConfigurationProcessor.Core as a Cake Tool
#tool nuget:?package=ConfigurationProcessor.Core&version=0.9.8                

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.ExcludedHosts.Clear();
   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": {
         "ExcludedHosts": {
            "Clear": true
         },
         "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.

.NET Type Example Configuration C# Equivalent
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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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 278 9/3/2023
1.11.0 456 3/25/2023
1.10.1 723 11/14/2022
1.9.0 609 11/12/2022
1.8.2 608 11/12/2022
1.7.2 682 11/4/2022
1.7.1 713 10/25/2022
1.6.1 689 10/24/2022
1.6.0 707 10/23/2022
1.5.1 705 10/22/2022
1.5.0 703 10/22/2022
1.4.0 1,044 10/11/2022
1.3.0 785 10/7/2022
1.2.1 753 10/6/2022
1.2.0 841 9/19/2022
1.1.1 795 9/16/2022
1.1.0 851 9/2/2022
1.0.0 1,198 7/7/2022
0.9.9 800 7/5/2022
0.9.8 778 7/5/2022
0.9.7 771 7/4/2022
0.9.5 796 7/3/2022
0.9.4 808 7/3/2022
0.9.3 800 7/3/2022
0.9.2 780 7/3/2022
0.9.1 787 7/2/2022