AppSettingsGenerator 1.0.0
dotnet add package AppSettingsGenerator --version 1.0.0
NuGet\Install-Package AppSettingsGenerator -Version 1.0.0
<PackageReference Include="AppSettingsGenerator" Version="1.0.0" />
paket add AppSettingsGenerator --version 1.0.0
#r "nuget: AppSettingsGenerator, 1.0.0"
// Install AppSettingsGenerator as a Cake Addin #addin nuget:?package=AppSettingsGenerator&version=1.0.0 // Install AppSettingsGenerator as a Cake Tool #tool nuget:?package=AppSettingsGenerator&version=1.0.0
AppSettingsGenerator
Quick start
Configure file from which settings should be generated in .csproj by adding:
<ItemGroup>
<AdditionalFiles Include="appsettings.json" />
</ItemGroup>
Generated classes
For given configuration file:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning"
}
}
}
Following classes will be generated.
namespace Configuration.Generated
{
[CompilerGenerated]
public partial class Logging
{
public LogLevel LogLevel { get; set; }
}
}
namespace Configuration.Generated
{
[CompilerGenerated]
public partial class LogLevel
{
public string Default { get; set; }
public string Microsoft { get; set; }
}
}
AppSettings.cs is the root configuration file.
Extend generated classes
All classes are marked as partial, therefore if needed they can be extended by custom methods / properties.
namespace Configuration.Generated
{
public partial class Logging
{
public bool Validate()
{
return !string.IsNullOrEmpty(this.LogLevel.Default);
}
}
}
Configuration Extensions
For every class generated there will be configuration extension generated to IServiceCollection interface. For given configuration
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime23": "Information"
}
}
}
Following extensions method will be generated:
public void ConfigureServices(IServiceCollection services)
{
services.ConfigureAllSections(Configuration);
services.ConfigureLogging(Configuration);
services.ConfigureLogLevel(Configuration);
}
Which basicly adds proper section to IServiceCollection.
public static void ConfigureLogging(this IServiceCollection services, IConfiguration configuration)
{
services.Configure<Configuration.Generated.Logging>(configuration.GetSection("Logging"));
}
ConfigureAllSections adds every sections at once. In our case it is equivalent to call these two lines:
services.ConfigureLogging(Configuration);
services.ConfigureLogLevel(Configuration);
Note: If you use ConfigureAllSections calling other methods are not necessary.
Diagnostics
- APG001 Error Configuration in .csproj is missing. To add it please follow instructions in section.
- APG002 Info Invalid identifier detected. If configuration file contains characters that are not valid in terms of C# class name or property name this information is reported. Given json property:
"Logging": {
"LogLevel": {
"Microsoft.Hosting.Lifetime23": "Information"
}
}
"Microsoft.Hosting.Lifetime": "Information" dot ('.') is not a valid character in property name, therefore it cannot be generated. In that case extension method to IConfiguration is generated. To use it Inject Microsoft.Extensions.Configuration.IConfiguration, add Configuration.Generated namespace and invoke IConfiguration.GetLoggingLogLevelMicrosoftHostingLifetime().
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 |
---|---|---|
1.0.0 | 411 | 5/7/2021 |