Skip to content

Configuration binding with nested types and readonly properties not working #77677

Closed

Description

@glucaci

Description

With #67258 the binding of immutable types is working but with nested types seams not to work.

Reproduction Steps

public record RootConfig(NestedConfig Nested);

public record NestedConfig(string MyProp);

public RootConfig GetConfiguration()
{
    IConfiguration configuration = new ConfigurationBuilder()
        .AddInMemoryCollection(new Dictionary<string, string?>
        {
            { "Nested:MyProp", "Dummy" }
        })
        .Build();

    return configuration.Get<RootConfig>();            
}

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.

public record RootConfig
{
    public NestedConfig Nested { get; init; }
}

Expected behavior

No exception.

Actual behavior

Throws InvalidOperationException.

Regression?

No response

Known Workarounds

Declare the Nested property with init modifier.

Configuration

.NET 7 RC2

Other information

No response

Activity

  1. ghost added
    untriaged
    on Oct 31, 2022
  2. tarekgh commented on Oct 31, 2022

    @tarekgh
    Member

    @SteveDunn will you have some spare time to look at this one?

  3. removed
    untriaged
    on Oct 31, 2022
  4. added this to the 8.0.0 milestone on Oct 31, 2022
  5. ghost added
    in-pr
    on Oct 31, 2022
  6. SteveDunn commented on Oct 31, 2022

    @SteveDunn
    Contributor

    @tarekgh / @glucaci - hi - I just got main and added this test and it passed without the exception described above

    image

    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)

  7. tarekgh commented on Oct 31, 2022

    @tarekgh
    Member

    @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
    
  8. tarekgh commented on Nov 1, 2022

    @tarekgh
    Member

    Interesting, it looks something has been changed in .NET 8.0 which fixed the issue. We need to understand what has changed.

  9. SteveDunn commented on Nov 1, 2022

    @SteveDunn
    Contributor

    It is this:

    image

    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.

  10. SteveDunn commented on Nov 1, 2022

    @SteveDunn
    Contributor

    Actually, it might be worth merging in as the test is useful as a regression.

  11. glucaci commented on Nov 1, 2022

    @glucaci
    Author

    It is this:

    image

    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.

    @tarekgh could this be cherry-picked into .NET 7?

  12. tarekgh commented on Nov 1, 2022

    @tarekgh
    Member

    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.

  13. ghost removed
    in-pr
    on Nov 1, 2022
  14. aradalvand commented on Nov 23, 2022

    @aradalvand

    Just encountered the same problem; such a shame that the fix didn't make it to 7.0...

    Considering the workaround is trivial

    What is the workaround, please?

  15. tarekgh commented on Nov 23, 2022

    @tarekgh
    Member

    @aradalvand

    In the issue description, it mentioned the following

    Known Workarounds
    Declare the Nested property with init modifier.
    
  16. ghost locked as resolved and limited conversation to collaborators on Dec 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions