ConfigurationProcessor.AspNetCore 0.9.1

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

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

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.

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 AddMyPlugin(this IServiceCollection services);
public IServiceCollection AddMyPlugin(this IServiceCollection services, string name);
public IServiceCollection AddMyPlugin(this IServiceCollection services, int count);

When given the configuration below, the extension method AddMyPlugin(IServiceCollection, int) is chosen.

{
   "Services": {
      "MyPlugin" : {
         "Count": 23
      }
   }
}

If the extension method is parameterless, use true instead of an object. The configuration method below will choose AddMyPlugin(IServiceCollection)

{
   "Services": {
      "MyPlugin" : 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 => {});:

{
   "MyPlugin" : true
}

The configuration below is equivalent to calling services.AddMyService(options => { options.Title = "abc" });:

{
   "MyPlugin" : {
      "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.

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 with NO OVERLOADS can be mapped with json objects.

public IServiceCollection AddMyService(this IServiceCollection services, Dictionary<string, int> values);
{
   "MyService" : {
      "Value1": 1,
      "Value2": 2
   }
}
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 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. 
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.12.0 139 9/3/2023
1.11.0 189 3/25/2023
1.10.1 331 11/14/2022
1.9.0 341 11/12/2022
1.8.2 329 11/12/2022
1.7.2 355 11/4/2022
1.7.1 364 10/25/2022
1.6.1 376 10/24/2022
1.6.0 397 10/23/2022
1.5.1 369 10/22/2022
1.5.0 360 10/22/2022
1.4.0 418 10/11/2022
1.3.0 374 10/7/2022
1.2.1 384 10/6/2022
1.2.0 399 9/19/2022
1.1.1 397 9/16/2022
1.1.0 387 9/2/2022
1.0.0 438 7/7/2022
0.9.9 427 7/5/2022
0.9.8 419 7/5/2022
0.9.7 409 7/4/2022
0.9.5 411 7/3/2022
0.9.4 430 7/3/2022
0.9.3 442 7/3/2022
0.9.2 413 7/3/2022
0.9.1 412 7/2/2022