AppSettingsGenerator 1.0.0

dotnet add package AppSettingsGenerator --version 1.0.0                
NuGet\Install-Package AppSettingsGenerator -Version 1.0.0                
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="AppSettingsGenerator" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AppSettingsGenerator --version 1.0.0                
#r "nuget: AppSettingsGenerator, 1.0.0"                
#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 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

License: MIT

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().

There are no supported framework assets in this 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
1.0.0 410 5/7/2021