Skip to content

ConfigurationBinder source generator generates code that doesn't compile when using StringValues #94547

Closed

Description

Description

When an Options object uses the StringValues type, the ConfigurationBinder Source Generator is generating code that doesn't compile.

Reproduction Steps

Build the following app:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
    <PublishAot>true</PublishAot>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="9.0.0-dev" />
    <PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.0-dev" />
  </ItemGroup>

</Project>
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Primitives;

IConfigurationSection c = new ConfigurationBuilder().Build().GetSection("Options");
c.Get<MyOptions>();

public class MyOptions
{
    public IDictionary<string, StringValues> DefaultValues { get; set; } = new Dictionary<string, StringValues>(StringComparer.OrdinalIgnoreCase);
}

Expected behavior

The project should build successfully.

Actual behavior

The build fails with:

Severity	Code	Description	Project	File	Line	Suppression State	Details
Error (active)	CS8518	An expression of type 'StringValues' can never match the provided pattern.	ConsoleApp101	C:\Users\eerhardt\source\repos\ConsoleApp101\ConsoleApp101\Microsoft.Extensions.Configuration.Binder.SourceGeneration\Microsoft.Extensions.Configuration.Binder.SourceGeneration.ConfigurationBindingGenerator\BindingExtensions.g.cs	68		

The generated code that fails looks like:

        public static void BindCore(IConfiguration configuration, ref global::Microsoft.Extensions.Primitives.StringValues instance, bool defaultValueIfNotFound, BinderOptions? binderOptions)
        {
            if (instance is not ICollection<string> temp) // this line has the build error
            {
                return;
            }

Regression?

No

Known Workarounds

No response

Configuration

No response

Other information

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions