镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

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

Closed

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

namespace ConsoleApp38
{
    internal class Program
    {
        static void Main(string[] args)
        {
            IHost host = new HostBuilder()
                .ConfigureAppConfiguration(builder =>
                {
                    builder.AddInMemoryCollection(new Dictionary<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);
        }
    }

    public class TestOptions
    {
        public IDictionary<string, string> DictionaryOptions { get; }
            = new Dictionary<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

Activity

  1. ghost added
    untriagedNew issue has not been triaged by the area owner
    on Sep 14, 2022
  2. added this to the 7.0.0 milestone on Sep 15, 2022
  3. ghost removed
    untriagedNew issue has not been triaged by the area owner
    on Sep 15, 2022
  4. self-assigned this
    on Sep 15, 2022
  5. added a commit that references this issue on Sep 15, 2022
    1c58769
  6. ghost added
    in-prThere is an active PR which will close this issue when it is merged
    on Sep 15, 2022
  7. eerhardt commented on Sep 15, 2022

    @eerhardt
    Member

    Thanks for the bug report, @jander-msft. I have a fix prepared and my plan is to ensure the fix gets in for 7.0-rc2.

    FYI - @ericstj

  8. added a commit that references this issue on Sep 15, 2022
    9ed97ee
  9. ghost removed
    in-prThere is an active PR which will close this issue when it is merged
    on Sep 15, 2022
  10. added a commit that references this issue on Sep 15, 2022
    59caa55
  11. added a commit that references this issue on Sep 16, 2022
    b9d050a
  12. ghost locked as resolved and limited conversation to collaborators on Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions