Closed mshwf closed 2 months ago
Tagging subscribers to this area: @dotnet/area-extensions-configuration See info in area-owners.md if you want to be subscribed.
Author: | mshwf |
---|---|
Assignees: | - |
Labels: | `untriaged`, `area-Extensions-Configuration` |
Milestone: | - |
This was fixed in https://github.com/dotnet/runtime/pull/94588
Can you make sure you update your package reference (if using the package) or update your SDK you aren't?
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.2" />
@ericstj I'm using the latest version of Visual Studio - v17.10.5 (so SDK is updated?), also added the mentioned package Microsoft.Extensions.Configuration.Binder (should I do something with it or it's for the SDK?), still have the same errors.
It could be a namespace collision in your project with some other types you haven't shared? I'm not sure, I cannot reproduce the error you're seeing.
Here's an isolated repro that shows it working: testBindEvent.zip
You can see the generated code has
public static void BindCore(IConfiguration configuration, ref global::MyOptions instance, bool defaultValueIfNotFound, BinderOptions? binderOptions)
{
ValidateConfigurationKeys(typeof(global::MyOptions), s_configKeys_MyOptions, configuration, binderOptions);
if (AsConfigWithChildren(configuration.GetSection("Events")) is IConfigurationSection section0)
{
instance.Events ??= new global::SomeEvent();
}
}
Description
This was discovered while I was upgrading to .NET 8 from .NET 6. There were numerous errors in this code:
I created a minimal project to reproduce, and I found the configuration binder fails (or may be expected to fail in this case) to generate the correct type when there is explicit type casting from an inherited member.
Reproduction Steps
Create these models (these are similar to how
JwtBearerOptions
is structured which confuses the configuration binder):and try to bind
MyOptions
from a configuration section:and add this to the CSProj file:
<EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
Try to build the project.Expected behavior
I'd expect the same behavior as the .NET 6 configuration system, and no compilation errors.
Actual behavior
Compilation error due to incorrect source code generated by the configuration binder:
This is the generated method that produces the error:
Regression?
No response
Known Workarounds
No response
Configuration
No response
Other information
No response