ConfigurationProcessor.DependencyInjection.Generator
0.2.2-beta.1
ConfigurationProcessor.Generator
Additional DetailsThis package is superseded by ConfigurationProcessor.Generator.
dotnet add package ConfigurationProcessor.DependencyInjection.Generator --version 0.2.2-beta.1
NuGet\Install-Package ConfigurationProcessor.DependencyInjection.Generator -Version 0.2.2-beta.1
<PackageReference Include="ConfigurationProcessor.DependencyInjection.Generator" Version="0.2.2-beta.1" />
paket add ConfigurationProcessor.DependencyInjection.Generator --version 0.2.2-beta.1
#r "nuget: ConfigurationProcessor.DependencyInjection.Generator, 0.2.2-beta.1"
// Install ConfigurationProcessor.DependencyInjection.Generator as a Cake Addin #addin nuget:?package=ConfigurationProcessor.DependencyInjection.Generator&version=0.2.2-beta.1&prerelease // Install ConfigurationProcessor.DependencyInjection.Generator as a Cake Tool #tool nuget:?package=ConfigurationProcessor.DependencyInjection.Generator&version=0.2.2-beta.1&prerelease
ConfigurationProcessor.DependencyInjection.Generator
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
{
[GenerateServiceRegistration("Services")]
internal static partial void RegisterServices(this IServiceCollection services, IConfiguration configuration);
[GenerateServiceRegistration("Services")]
public static partial void AddServicesFromConfiguration(this WebApplicationBuilder builder);
}
Create a static partial method in a partial class decorated with GenerateServiceRegistrationAttribute
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 GenerateServiceRegistrationAttribute
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 | 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
- 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.2.2-beta.1 | 99 | 9/15/2023 | |
0.2.1-beta.2 | 92 | 9/7/2023 | |
0.2.1-beta.1 | 100 | 9/6/2023 | |
0.2.0-beta.1 | 87 | 9/6/2023 | |
0.1.0-beta.6 | 97 | 9/5/2023 | |
0.1.0-beta.5 | 90 | 9/5/2023 | |
0.1.0-beta.4 | 85 | 9/5/2023 | |
0.1.0-beta.3 | 88 | 9/5/2023 | |
0.1.0-beta.2 | 88 | 9/5/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