Skip to content

[API Proposal]: Support of configuration options with a full qualified section key like in the old days of .NET Framework #95678

Open
@HHobeck

Description

@HHobeck

Background and motivation

Dear community,

I came up with the idea to extend the Microsoft.Extensions.Options model to support the configuration options with a full qualified section key to resolve the IOptions dependencies automatically. This has the advantage that you don't need to register the configuration classes explicit (and map it to an arbitrary section in your configuration) in the application startup each time. It is useful especially if you are not aware of third party libraries and want to change the behavior or fine tune the settings (via IOptions) in a later stage without recompilation.

Hope this finds a way in .net core because it was a breaking change compared to the old world of .NET Framework and left the user without the option to change the behavior of how the settings are resolved.

Thank you.

API Proposal

Possible implementation:

using System;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.Extensions.Options;

public static class OptionsWithFullQualifiedSectionKeyComposition
{
    public static void Register(IServiceCollection serviceCollection)
    {
        if (serviceCollection is null) throw new ArgumentNullException(nameof(serviceCollection));

        serviceCollection.AddSingleton(typeof(IConfigureOptions<>), typeof(ConfigureOptionsWithFullQualifiedSectionKey<>));
        serviceCollection.AddSingleton(
            typeof(IOptionsChangeTokenSource<>), typeof(ConfigurationChangeTokenSourceWithFullQualifiedSectionKey<>)
        );
    }
}

public class ConfigureOptionsWithFullQualifiedSectionKey<TOptions>
    : ConfigureFromConfigurationOptions<TOptions> where TOptions : class
{
    public ConfigureOptionsWithFullQualifiedSectionKey(IConfiguration configuration)
        : base(configuration?.GetSection(typeof(TOptions).FullName)!)
    {
    }
}

public class ConfigurationChangeTokenSourceWithFullQualifiedSectionKey<TOptions>
    : ConfigurationChangeTokenSource<TOptions>
{
    public ConfigurationChangeTokenSourceWithFullQualifiedSectionKey(IConfiguration configuration)
        : base(configuration?.GetSection(typeof(TOptions).FullName)!)
    {
    }
}

API Usage

Possible usage:

internal class Program
{
    static void Main(string[] arguments)
    {
        IHostBuilder hostBuilder = Host.CreateDefaultBuilder(arguments);
        hostBuilder.ConfigureServices(
            serviceCollection => OptionsWithFullQualifiedSectionKeyComposition.Register(serviceCollection)
        );

        using IHost host = hostBuilder.Build();
        host.Run();
    }
}

Alternative Designs

No response

Risks

This has no risks and can be seen as an option to the user to change the way how the configuration are loaded.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions