You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
IConfiguration type binding for properties in Options pattern. #42035
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.
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:
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.
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.
Activity
Dotnet-GitSync-Bot commentedon Sep 9, 2020
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.
pinkfloydx33 commentedon Sep 10, 2020
You can register an
I[Post]ConfigureOptionsimplementation that assigns to your property:You can either user
IConfigureOptions(1) orIPostConfigureOptions(2)--or technically both. You register this with the service collections as: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. AllIConfigureOptionsrun first, followed by allIPostConfigureOptions--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:
The section name can then be read from your configuration (5)--or the default value. You'd register it using:
While it's not built-in and automatic, it's definitely a way to achieve what you are looking to accomplish.
armandjnbg commentedon Sep 14, 2020
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.
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
KalleOlaviNiemitalo commentedon Jun 11, 2022
IConfigurationSection binding works in .NET 6.0 already, although IConfiguration binding doesn't:
Output:
Implemented here:
runtime/src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs
Lines 344 to 348 in 4822e3c
It originally came from aspnet/Configuration@3480095.
rosebyte commentedon Mar 2, 2026
Triage: we can investigate why IConfiguration isn't supported now and consider support it.
svick commentedon Mar 20, 2026
After a discussion with the team, I'm closing this. While using
IConfigurationthis way makes logical sense, that interface is also sometimes used to represent the whole configuration (e.g. in DI), so it could be confusing.IConfigurationSectionis a perfect fit and already works, so nothing needs to be changed.