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
Configuration binding with nested types and readonly properties not working #77677
In the above case the following exception will be thrown:
Cannot create instance of type 'RootConfig' because parameter 'Nested' has no matching config.
Each parameter in the constructor that does not have a default value must have a corresponding config entry.
If the Nested property is declared with init modifier like bellow it will work.
@SteveDunn thanks for the follow up. I am trying that with .NET 7.0 rc2 and I am seeing it failing. I'll try the rtm bits to see if it reproduces there.
FrameworkDescription: .NET 7.0.0-rc.2.22472.3
RuntimeIdentifier: win10-x64
Unhandled exception. System.InvalidOperationException: Cannot create instance of type 'NetCoreApp.Program+RootConfig' because parameter 'Nested' has no matching config. Each parameter in the constructor that does not have a default value must have a corresponding config entry.
at Microsoft.Extensions.Configuration.ConfigurationBinder.BindParameter(ParameterInfo parameter, Type type, IConfiguration config, BinderOptions options)
at Microsoft.Extensions.Configuration.ConfigurationBinder.CreateInstance(Type type, IConfiguration config, BinderOptions options)
at Microsoft.Extensions.Configuration.ConfigurationBinder.BindInstance(Type type, BindingPoint bindingPoint, IConfiguration config, BinderOptions options)
at Microsoft.Extensions.Configuration.ConfigurationBinder.Get(IConfiguration configuration, Type type, Action`1 configureOptions)
at Microsoft.Extensions.Configuration.ConfigurationBinder.Get[T](IConfiguration configuration, Action`1 configureOptions)
at Microsoft.Extensions.Configuration.ConfigurationBinder.Get[T](IConfiguration configuration)
at NetCoreApp.Program.BindWithNestedTypesWithReadOnlyProperties() in C:\Temp\NetCoreApp\Program.cs:line 114
at NetCoreApp.Program.Main(String[] args) in C:\Temp\NetCoreApp\Program.cs:line 159
Actually, it might be worth merging in as the test is useful as a regression.
Agree. Thanks for pointing at the change that fixed this issue.
could this be cherry-picked into .NET 7?
.NET 7.0 is done. It is going to be released in a few days. We may consider servicing this later if proven it is a blocker to important scenarios. Considering the workaround is trivial and it is not a regression, this will be difficult to meet the servicing bar.
Activity
tarekgh commentedon Oct 31, 2022
@SteveDunn will you have some spare time to look at this one?
SteveDunn commentedon Oct 31, 2022
@tarekgh / @glucaci - hi - I just got main and added this test and it passed without the exception described above
Admittedly, this was a very quick exercise as my time is rather limited with my day job.
Have I missed something?
(PR added - please feel free to modify it so that it breaks, and I'll attempt a fix when time permits - #77693)
tarekgh commentedon Oct 31, 2022
@SteveDunn thanks for the follow up. I am trying that with .NET 7.0 rc2 and I am seeing it failing. I'll try the rtm bits to see if it reproduces there.
tarekgh commentedon Nov 1, 2022
Interesting, it looks something has been changed in .NET 8.0 which fixed the issue. We need to understand what has changed.
SteveDunn commentedon Nov 1, 2022
It is this:
Left is RC2, right is Main as of yesterday (31st October '22)
The commit on the right is ca0d3e4d by @Saalvage
I'm happy if you want to close this PR.
SteveDunn commentedon Nov 1, 2022
Actually, it might be worth merging in as the test is useful as a regression.
glucaci commentedon Nov 1, 2022
@tarekgh could this be cherry-picked into .NET 7?
tarekgh commentedon Nov 1, 2022
Agree. Thanks for pointing at the change that fixed this issue.
.NET 7.0 is done. It is going to be released in a few days. We may consider servicing this later if proven it is a blocker to important scenarios. Considering the workaround is trivial and it is not a regression, this will be difficult to meet the servicing bar.
aradalvand commentedon Nov 23, 2022
Just encountered the same problem; such a shame that the fix didn't make it to 7.0...
What is the workaround, please?
tarekgh commentedon Nov 23, 2022
@aradalvand
In the issue description, it mentioned the following