Skip to content

IConfiguration type binding for properties in Options pattern. #42035

Closed as not planned

Description

@armandjnbg

Hello everybody,

I defined the following configuration option class for use with Options pattern:

public class RouteOptions
{
   public string Name { get; set; }
   public string Local { get; set; }
   public string Remote { get; set; }
   public string HttpClientName { get; set; }
   . . .
   public IConfiguration Extra { get; set; }
}

The "Extra" field by my design would include various extra settings that will be accessed by business logic located outside of my code, e.g. plugins written by other developers. I was expecting this to work, but to my surprise, this property is never assigned.

Probably it is very easy to bind this field it does not require any conversion, just assign GetSection("propertyName") to the property.

Is it possible to introduce this functionality?

I think it is essential.

With best regards,
Arman

Activity

  1. Dotnet-GitSync-Bot commented on Sep 9, 2020

    @Dotnet-GitSync-Bot
    Collaborator

    I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

  2. added
    untriaged
    on Sep 9, 2020
  3. pinkfloydx33 commented on Sep 10, 2020

    @pinkfloydx33

    You can register an I[Post]ConfigureOptions implementation that assigns to your property:

    public class ConfigureRouteOptions : IConfigureOptions<RouteOptions>, IPostConfigureOptions<RouteOptions>
    {
       private readonly IConfiguration _config;
       public ConfigureRouteOptions(IConfiguration config)
          => _config = config.GetSection("Whatever"); // 4
    
       public void Configure(RouteOptions opts) // 1
       {
             _config.Bind(opts); // 3
             opts.Extra = _config.GetSection("Extra"); 
       }
    
      public void PostConfigure(string name, RouteOptions opts) // 2
      {
            if(opts.Extra is null) 
               opts.Extra = _config.GetSection("Extra");
      } 
    }

    You can either user IConfigureOptions (1) or IPostConfigureOptions (2)--or technically both. You register this with the service collections as:

    services.ConfigureOptions<ConfigureRouteOptions>();

    If you've already bound your configuration (via a call to Configure<RouteOptions>) then binding the "main" properties manually (3) wouldn't be necessary. In fact you'd probably need to guard properly against that case to be sure you don't discard any prior configuration. All IConfigureOptions run first, followed by all IPostConfigureOptions--so just something to keep in mind. I'd probably select the Post-Configure version myself.

    The only downside here is that your root section name (4) is hard-coded. That may not be a problem, but if it is you can provide the configurator with its own options class. Something like:

    public class ConfiguratorOpts
    {
       public string RootSection { get; set; } = "DefaultValue";
    }
    
    public class ConfigureRouteOptions : IConfigureOptions<RouteOptions>, IPostConfigureOptions<RouteOptions>
    {
       private readonly IConfiguration _config;
       public ConfigureRouteOptions(IConfiguration config, IOptions<ConfiguratorOpts> opts /* 5 */)
          => _config = config.GetSection(opts.Value.RootSection); // 4
    
       public void Configure(RouteOptions opts) // 1
       {
             _config.Bind(opts); // 3
             opts.Extra = _config.GetSection("Extra"); 
       }
    
      public void PostConfigure(string name, RouteOptions opts) // 2
      {
            if(opts.Extra is null) 
               opts.Extra = _config.GetSection("Extra");
      } 
    }

    The section name can then be read from your configuration (5)--or the default value. You'd register it using:

    services.Configure<ConfiguratorOpts>(o => o.RootSection = "SomethingElse");
    services.ConfigureOptions<ConfigureRouteOptions>();

    While it's not built-in and automatic, it's definitely a way to achieve what you are looking to accomplish.

  4. removed
    untriaged
    on Sep 11, 2020
  5. added this to the Future milestone on Sep 11, 2020
  6. armandjnbg commented on Sep 14, 2020

    @armandjnbg
    Author

    Thanks,

    Your solution is applicable in my case, but my case is a bit more complex because RouteOptions include inner RouteOptions.
    Plus RootOptions themself reside inside of another configuration element RouteMap. So it a tree-like structure in the config. So I don't have a section name for reading each of that RouteOption representation in the configuration.

        public class RouteOptions
    {
    	public string Name { get; set; }
    	public string Local { get; set; }
    	public string Remote { get; set; }
    	public string HttpClientName { get; set; }
    
    	public bool Validate { get; set; } = true;
    	public bool Audit { get; set; } = true;
    
    	public ClaimValidationOptions[] Claims { get; set; }
    
    	IConfigurationSection Extra { get; set; }
    
                // inner elements here
    	public List<RouteOptions> Inner { get; set; }
    }
    

    I will write a custom code for reading this tree from the configuration, based on recursive call to IPostConfigureOptions probably.

    But I was thinking that it would be good to include out of the box IConfigurationSection binding somewhere in the next versions of .NET Core, this doesn't seem like require significant changes to the existing code of configuration binder.

    So I guess this is a feature request. :-)

    With best regards,
    Arman

  7. KalleOlaviNiemitalo commented on Jun 11, 2022

    @KalleOlaviNiemitalo

    it would be good to include out of the box IConfigurationSection binding somewhere in the next versions of .NET Core

    IConfigurationSection binding works in .NET 6.0 already, although IConfiguration binding doesn't:

    using System;
    using System.Collections.Generic;
    using Microsoft.Extensions.Configuration;
    
    IConfiguration configuration = new ConfigurationBuilder()
        .AddInMemoryCollection(
            new KeyValuePair<string, string>[]
            {
                new KeyValuePair<string, string>("Name", "Kaur"),
                new KeyValuePair<string, string>("Extra:hello", "world"),
            })
         .Build();
    var routeOptions = configuration.Get<RouteOptions>();
    
    Console.WriteLine(routeOptions.Name);
    Console.WriteLine(routeOptions.Extra["hello"]);
    
    public class RouteOptions
    {
       public string Name { get; set; }
       public string Local { get; set; }
       public string Remote { get; set; }
       public string HttpClientName { get; set; }
       public IConfigurationSection Extra { get; set; }
    }

    Output:

    Kaur
    world
    

    Implemented here:

    // if binding IConfigurationSection, break early
    if (type == typeof(IConfigurationSection))
    {
    return config;
    }

    It originally came from aspnet/Configuration@3480095.

  8. self-assigned this
    on Mar 2, 2026
  9. rosebyte commented on Mar 2, 2026

    @rosebyte
    Member

    Triage: we can investigate why IConfiguration isn't supported now and consider support it.

  10. added
    in-pr
    on Mar 17, 2026
  11. svick commented on Mar 20, 2026

    @svick
    Member

    After a discussion with the team, I'm closing this. While using IConfiguration this way makes logical sense, that interface is also sometimes used to represent the whole configuration (e.g. in DI), so it could be confusing. IConfigurationSection is a perfect fit and already works, so nothing needs to be changed.

  12. modified the milestones: Future, 11.0.0 on Mar 20, 2026
  13. locked and limited conversation to collaborators on Apr 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-Extensions-Configurationfeature-requestin-pr

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions