Skip to content

Options binding fails on get-only IDictionary<,> properties #75626

Description

@jander-msft

Description

When binding an options type that contains a public get-only property of type IDictionary<,>, the binding is skipped and the dictionary is not populated.

Reproduction Steps

namespaceConsoleApp38{internalclassProgram{staticvoidMain(string[]args){IHosthost=newHostBuilder().ConfigureAppConfiguration(builder =>{builder.AddInMemoryCollection(newDictionary<string,string?>(){{"DictionaryOptions:Key1","Value1"}});}).ConfigureServices((context,services)=>{services.Configure<TestOptions>(context.Configuration);}).Build();IOptions<TestOptions>options=host.Services.GetRequiredService<IOptions<TestOptions>>();IDictionary<string,string>dictionaryOptions=options.Value.DictionaryOptions;Console.WriteLine("Count = {0}",dictionaryOptions.Count);}}publicclassTestOptions{publicIDictionary<string,string>DictionaryOptions{get;}=newDictionary<string,string>();}}

Expected behavior

The dictionary property on the options instance should have one key-value pair. Console will write out "Count = 1".

Actual behavior

The dictionary property on the options instance has not key-value pairs. Console will write out "Count = 0".

Regression?

Yes, this worked in .NET 7 Preview 7, all prior previews, and all prior .NET (Core) versions.

Known Workarounds

Changing the property type to Dictionary<,> seems to work around this issue.

Configuration

Windows 11 x64 .NET SDK 7.0.100-rc.1.22431.12

Not specific to this configuration, it is failing on all OSes and all architectures in the dotnet-monitor PR builds when attempting to update to .NET 7 RC 1.

Other information

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions