Repository navigation
Closed
Milestone
Description
Description
Trying to use the ConfigurationBinder Source Generator with a StackExchange.Redis ConfigurationOptions type (which wasn't necessarily designed to work with IConfiguration), produces code that doesn't compile.
Reproduction Steps
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
<NoWarn>$(NoWarn);SYSLIB1100;SYSLIB1101</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.0-rtm.23511.16" />
<PackageReference Include="StackExchange.Redis" Version="2.6.122" />
</ItemGroup>
</Project>using Microsoft.Extensions.Configuration;
using StackExchange.Redis;
Console.WriteLine("Hello, World!");
static void BindToConfiguration(ConfigurationOptions options, IConfiguration configuration)
{
var configurationOptionsSection = configuration.GetSection("ConfigurationOptions");
configurationOptionsSection.Bind(options);
}Expected behavior
The project should build cleanly.
Actual behavior
Severity Code Description Project File Line Suppression State Details
Error (active) CS0246 The type or namespace name 'SslClientAuthenticationOptions' could not be found (are you missing a using directive or an assembly reference?) ConsoleApp101 C:\Users\eerhardt\source\repos\ConsoleApp101\ConsoleApp101\Microsoft.Extensions.Configuration.Binder.SourceGeneration\Microsoft.Extensions.Configuration.Binder.SourceGeneration.ConfigurationBindingGenerator\BindingExtensions.g.cs 1400
The code that doesn't compile looks like:
public static void BindCore(IConfiguration configuration, ref Func<string, SslClientAuthenticationOptions> instance, bool defaultValueIfNotFound, BinderOptions? binderOptions)
{
ValidateConfigurationKeys(typeof(Func<string, SslClientAuthenticationOptions>), s_configKeys_FuncStringSslClientAuthenticationOptions, configuration, binderOptions);
if (AsConfigWithChildren(configuration.GetSection("Method")) is IConfigurationSection section263)
{
MethodInfo? temp265 = instance.Method;
if (temp265 is not null)
{
BindCore(section263, ref temp265, defaultValueIfNotFound: false, binderOptions);
}
else
{
throw new InvalidOperationException("Cannot create instance of type 'System.Reflection.MethodInfo' because it is missing a public instance constructor.");
}
}
}Regression?
No response
Known Workarounds
Add global using System.Net.Security; to the code.
Configuration
No response
Other information
No response
Activity
ericstj commentedon Oct 26, 2023
Here's the type in question: https://github.com/StackExchange/StackExchange.Redis/blob/5504ed9a93f729f204e6cb53280cb4efa1ccd390/src/StackExchange.Redis/ConfigurationOptions.cs
That exposes a public
Func<string,SslClientAuthenticationOptions>property:https://github.com/StackExchange/StackExchange.Redis/blob/5504ed9a93f729f204e6cb53280cb4efa1ccd390/src/StackExchange.Redis/ConfigurationOptions.cs#L573
Right - I don't think it's normal to expose a Func on a type being bound with config.
I wonder what the generated code that's trying to bind a
MethodInfodoes 😆 -- I can't imagine that working. I'm surprised you didn't see a diagnostic telling you this type wasn't supported.Probably we need to avoid generating code that doesn't compile and better detect these unsupported types and skip them or let the user specify they wish to skip them.
ericstj commentedon Oct 26, 2023
Here's a simplified repro:
configFunc.zip
This does emit a mess of diagnostics to let you know you're not doing something that will be supported.
We can reduce all that noise by just using a generic type we define ourselves, and not Func (which shouldn't be used in a config type).
This will reproduce the missing namespace. The bug seems to be that the source generator won't add usings if they are only needed for type parameters.
Now this isn't really a common case - because the type parameter is completely unused in the types the binder knows about.
If I add a member that uses the type parameter, the error goes away and the generator produces good code.
Similarly, I can workaround the original issue if I add code that binds to the missing type.
eerhardt commentedon Oct 27, 2023
@ericstj - I see this bug is in
9.0.0right now. Per our discussion today, this bug should be fixed in 8.0.x servicing right? We will fully qualify all the types we generate code for?ericstj commentedon Oct 27, 2023
Yes - we will need to fix this in servicing. First fix in main then backport. I think that this bug will actually be fixed with #93498 - by eliminating any dependence on
usingstatements.ericstj commentedon Nov 3, 2023
I confirmed that my minimal repro with MyGeneric is fixed now with #93498.
We now generate this as:
We still generate binding logic for the Func and all of the reflection types, but those are generated and compile correctly. They will go away once we trim out delegates : #94117