ConfigurationProcessor.Generator 0.6.0-beta.1

This is a prerelease version of ConfigurationProcessor.Generator.
dotnet add package ConfigurationProcessor.Generator --version 0.6.0-beta.1                
NuGet\Install-Package ConfigurationProcessor.Generator -Version 0.6.0-beta.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.Generator" Version="0.6.0-beta.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ConfigurationProcessor.Generator --version 0.6.0-beta.1                
#r "nuget: ConfigurationProcessor.Generator, 0.6.0-beta.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.Generator as a Cake Addin
#addin nuget:?package=ConfigurationProcessor.Generator&version=0.6.0-beta.1&prerelease

// Install ConfigurationProcessor.Generator as a Cake Tool
#tool nuget:?package=ConfigurationProcessor.Generator&version=0.6.0-beta.1&prerelease                

ConfigurationProcessor.Generator

NuGet

This packages uses source generation to generate dependency injection registration methods based on the appsettings.config configuration. This is still in beta and the current version partially supports the configuration mechanisms available in the ConfigurationProcessor.DependencyInjection package.

Usage

Code

internal static partial class ServiceRegistrations
{
   [GenerateConfiguration("Services")]
   internal static partial void RegisterServices(this IServiceCollection services, IConfiguration configuration);

   [GenerateConfiguration("Services")]
   public static partial void AddServicesFromConfiguration(this WebApplicationBuilder builder);
}

Create a static partial method in a partial class decorated with GenerateConfigurationAttribute and that contains an IServiceCollection and IConfiguration parameters or a single parameter with a parameter type that contains a single IServiceCollection and IConfiguration property.

The GenerateConfigurationAttribute requires a single argument , configurationSection which will be the configuration section in the appsettings.json to read from.

Sample configuration

{
   "Logging": {
      "LogLevel": {
         "Default": "Information",
         "Microsoft.AspNetCore": "Warning"
      }
   },
   "AllowedHosts": "*",
   "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"
      },
      "Controllers": true,
      "EndpointsApiExplorer": true,
      "SwaggerGen": true
   }
}

Sample generated code

public static partial void AddServicesFromConfiguration(this global::Microsoft.AspNetCore.Builder.WebApplicationBuilder builder)
{
    var servicesSection = builder.Configuration.GetSection("Services");
    if (!servicesSection.Exists())
    {
        return;
    }
         
    var sectionConfigure = servicesSection.GetSection("Configure<Microsoft.AspNetCore.Builder.CookiePolicyOptions>");
    if (sectionConfigure.Exists())
    {
        builder.Services.Configure<Microsoft.AspNetCore.Builder.CookiePolicyOptions>(options =>
        {
            options.HttpOnly = sectionConfigure.GetValue<Microsoft.AspNetCore.CookiePolicy.HttpOnlyPolicy>("HttpOnly");
            options.Secure = sectionConfigure.GetValue<Microsoft.AspNetCore.Http.CookieSecurePolicy>("Secure");
        });
    }
         
    if (servicesSection.GetValue<bool>("Controllers"))
    {
        builder.Services.AddControllers();
    }
         
    if (servicesSection.GetValue<bool>("EndpointsApiExplorer"))
    {
        builder.Services.AddEndpointsApiExplorer();
    }
         
    var sectionHsts = servicesSection.GetSection("Hsts");
    if (sectionHsts.Exists())
    {
        builder.Services.AddHsts(options =>
        {
            if (sectionHsts.GetValue<bool>("ExcludedHosts:Clear"))
            {
                options.ExcludedHosts.Clear();
            }
            options.IncludeSubDomains = sectionHsts.GetValue<System.Boolean>("IncludeSubDomains");
            options.Preload = sectionHsts.GetValue<System.Boolean>("Preload");
            options.MaxAge = sectionHsts.GetValue<System.TimeSpan>("MaxAge");
        });
    }
         
    if (servicesSection.GetValue<bool>("Logging"))
    {
        builder.Services.AddLogging();
    }
         
    var sectionOpenTelemetryTracing = servicesSection.GetSection("OpenTelemetryTracing");
         
    if (servicesSection.GetValue<bool>("SwaggerGen"))
    {
        builder.Services.AddSwaggerGen();
    }
}

Issues and Contributions

Contributions are welcome. Please open an issue if you encounter a problem or have a use case you want to support that is not currently supported.

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.
  • .NETStandard 2.0

    • No dependencies.

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
0.6.0-beta.1 66 9/27/2023
0.5.0-beta.1 64 9/26/2023
0.4.3-beta.1 68 9/26/2023
0.4.2-beta.1 63 9/26/2023
0.4.1-beta.1 61 9/26/2023
0.4.0-beta.1 68 9/19/2023
0.3.0-beta.1 67 9/19/2023

v1.13.0
  - Refactor code to support source generation package
v1.12.0
  - Specify license as Apache 2.0
v1.11.0
  - Added support for mapping to Dictionary<T, Action<TArg>>
v1.10.0
  - Support Action delegates of any number of parameters as configuration targets.
v1.9.0
  - Support Action delegates of up to 7 generic arguments as configuration targets.
v1.8.1
  - Support Action<T1, T2> and Action<T1, T2, T3> as configuration targets.
v1.7.2
  - Added special handling for retrieving ConnectionStrings
  - Lambda parameters can now be in any position
v1.6.1
  - Fixed dynamic invocation issues with optional parameters
v1.6.0
  - Added IConfigurationProcessor interface to dynamically call methods
v1.5.1
  - Fixed exception with optional parameters
v1.5.0
  - Improve selection of single parameter overload method.
  - Improve binding to readonly properties
v1.4.0
  - Improve discovery of executable methods to include implemented interfaces
v1.3.0
  - Add support for disambiguating overloads with different array types.
  - Prefer overloads with the most matching parameters
v1.2.1
  - Fix MissingMethodException when there exists a candidate method with no parameters is not chosen correctly.
v1.2.0
  - Added interception for missing extension method event
v1.1.1
  - Ignore TypeLoadException when scanning for extension methods
v1.1.0
  - Added support for binding with interface configuration actions
v1.0.0
  - Initial release