Support IConfiguration as a bindable property type in options classes#125656
Closed
Support IConfiguration as a bindable property type in options classes#125656
IConfiguration as a bindable property type in options classes#125656Conversation
…nder Co-authored-by: svick <287848+svick@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Add IConfiguration type binding for properties in Options pattern
Support Mar 17, 2026
IConfiguration as a bindable property type in options classes
69a1c27 to
934b6e7
Compare
…IConfigurationSection casts Co-authored-by: svick <287848+svick@users.noreply.github.com>
This was referenced Mar 17, 2026
Open
…n binding Co-authored-by: svick <287848+svick@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Adds first-class support for binding IConfiguration-typed properties in ConfigurationBinder (and its source generator), addressing the surprising “silent no-op” behavior when options classes include IConfiguration properties.
Changes:
- Extend
ConfigurationBinder.BindInstanceto short-circuit forIConfiguration(similar toIConfigurationSection). - Update the binding source generator to model/emit
IConfigurationvsIConfigurationSectioncorrectly. - Add/adjust tests validating binding behavior for
IConfigurationproperties andGet<T>()scenarios.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs | Adds/reshuffles tests for IConfiguration binding and Get<IConfiguration>() container scenarios. |
| src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.TestClasses.cs | Introduces new options/test classes for IConfiguration and renames existing section-focused option type. |
| src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs | Extends early-exit binding logic to directly bind IConfiguration. |
| src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Specs/Types/SimpleTypeSpec.cs | Adds a discriminator (IsIConfiguration) to distinguish spec flavor in generator pipeline. |
| src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/CoreBindingHelpers.cs | Emits direct-return for IConfiguration and keeps cast/throw behavior for IConfigurationSection. |
| src/libraries/Microsoft.Extensions.Configuration.Binder/gen/ConfigurationBindingGenerator.Parser.cs | Produces the new IsIConfiguration spec variant when parsing IConfiguration properties. |
...libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs
Show resolved
Hide resolved
...libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs
Show resolved
Hide resolved
svick
approved these changes
Mar 18, 2026
tarekgh
reviewed
Mar 18, 2026
src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs
Show resolved
Hide resolved
This was referenced Mar 19, 2026
Member
|
After a discussion with the team, I'm closing this. While using |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds support for binding
IConfigurationproperties inConfigurationBinderand the configuration binding source generator, fixing #42035.The original discussion claimed that handling
IConfigurationSectionis sufficient. But that didn't consider that users naturally expectIConfiguration-typed properties to work as well — they do not know that usingIConfigurationSectionspecifically is necessary, and the silent failure is confusing.Changes
src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.csBindInstanceto also short-circuit when bindingIConfiguration(not justIConfigurationSection). The config object is assigned directly to the binding point.Source generator (
gen/)IsIConfigurationproperty toConfigurationSectionSpecto distinguish betweenIConfigurationandIConfigurationSectionspecs.ConfigurationSectionSpec { IsIConfiguration = true }when the type isIConfiguration.ConfigurationSectionSpec { IsIConfiguration: true }that emitsreturn configuration;directly, without the cast-to-IConfigurationSectionguard.EmitCastToIConfigurationSectionhelper toEmitCastToIConfigurationSectionOrThrowto better reflect its behavior.Tests (
tests/Common/)The diff of tests is a bit confusing, mostly because of the move of
CanBindIConfigurationSectionWithDerivedOptionsSectionto a better location in the file.Renamed
ConfigurationInterfaceOptionstoOptionsWithIConfigurationSectionfor clarity.Added
OptionsWithIConfigurationandDerivedOptionsWithIConfigurationtest classes.Added
CanBindIConfigurationtest verifying that properties of typeIConfigurationare bound correctly (including via allGet<T>overloads).Added
CanBindIConfigurationWithDerivedOptionsSectiontest verifying nested binding throughIConfigurationproperties.Moved the existing
CanBindIConfigurationSectionWithDerivedOptionsSectiontest.Added
GetIConfigurationtest verifyingGet<IConfiguration>(),Get<List<IConfiguration>>(), andGet<Dictionary<string, List<IConfiguration>>>()all work correctly.Fixes IConfiguration type binding for properties in Options pattern. #42035
Original prompt