Table of Contents

Class ConfigurationHelper

Namespace
Arc4u.Configuration
Assembly
Arc4u.Configuration.dll

Helpers to build an IConfiguration from files or embedded resources and to register the ApplicationConfig options.

public static class ConfigurationHelper
Inheritance
ConfigurationHelper
Inherited Members

Examples

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddApplicationConfig(builder.Configuration, "Application.Configuration");

Methods

AddApplicationConfig(IServiceCollection, IConfiguration, string)

Reads the ApplicationConfig from a configuration section, validates it and registers it as options. The same validation as AddApplicationConfig(IServiceCollection, Action<ApplicationConfig>) applies.

public static void AddApplicationConfig(this IServiceCollection services, IConfiguration configuration, string sectionName = "Application.Configuration")

Parameters

services IServiceCollection

The service collection.

configuration IConfiguration

The configuration that contains the section.

sectionName string

The name of the section. The default is Application.Configuration.

Examples

// appsettings.json:
// "Application.Configuration": {
//   "ApplicationName": "MyApp",
//   "Environment": { "Name": "Development", "LoggingName": "MyApp", "TimeZone": "Europe/Brussels" }
// }
services.AddApplicationConfig(configuration);

Exceptions

ArgumentNullException

configuration is null.

ArgumentException

sectionName is empty.

ConfigurationException

The section is missing or empty, or one or more of the required values are missing.

AddApplicationConfig(IServiceCollection, Action<ApplicationConfig>)

Validates and registers the ApplicationConfig options. The application name, the environment name, the logging name and the time zone must all be provided.

public static void AddApplicationConfig(this IServiceCollection services, Action<ApplicationConfig> option)

Parameters

services IServiceCollection

The service collection.

option Action<ApplicationConfig>

The action that fills the ApplicationConfig.

Examples

services.AddApplicationConfig(config =>
{
    config.ApplicationName = "MyApp";
    config.Environment.Name = "Development";
    config.Environment.LoggingName = "MyApp";
    config.Environment.TimeZone = "Europe/Brussels";
});

Exceptions

ConfigurationException

One or more of the required values are missing; the message lists all of them.

GetConfigurationFromFile(params string[])

Load json files for the configuration and returns a IConfiguration so we can extract the settings. More than one file is possible.

public static IConfiguration GetConfigurationFromFile(params string[] configFiles)

Parameters

configFiles string[]

Returns

IConfiguration

GetConfigurationFromResourceStream(Assembly, params string[])

Builds an IConfiguration from JSON files embedded as resources in assembly. Unlike the overload taking assembly names, it works with trimming and native AOT.

public static IConfiguration GetConfigurationFromResourceStream(Assembly assembly, params string[] resourceNames)

Parameters

assembly Assembly

The assembly holding the resources, typically typeof(Program).Assembly.

resourceNames string[]

The full names of the resources to load, in order: a later resource overrides the values of an earlier one.

Returns

IConfiguration

The configuration built from the resources. Resources that are not found in the assembly are ignored.

Examples

var configuration = ConfigurationHelper.GetConfigurationFromResourceStream(typeof(Program).Assembly, "MyApp.appsettings.json");

Exceptions

ArgumentNullException

assembly or resourceNames is null.

GetConfigurationFromResourceStream(params string[])

Builds an IConfiguration from JSON files embedded as resources in assemblies loaded by name.

[RequiresUnreferencedCode("The assemblies are loaded by name and can be removed by trimming. Use the overload taking an Assembly, e.g. typeof(Program).Assembly.")]
public static IConfiguration GetConfigurationFromResourceStream(params string[] fileTypes)

Parameters

fileTypes string[]

The resources to load. Each item has the form "AssemblyName, full.resource.name".

Returns

IConfiguration

The configuration built from the resources. Resources that are not found in the assembly are ignored.

Remarks

The assemblies are loaded with Load(string), which trimming cannot follow: an assembly only named here can be removed. Use GetConfigurationFromResourceStream(Assembly, params string[]) in a trimmed or native AOT application.

Exceptions

ApplicationException

An item of fileTypes is not made of exactly two comma-separated parts.