Closed
Labels
area-Extensions-Configurationbugin-prThere is an active PR which will close this issue when it is mergedsource-generatorIndicates an issue with a source generator feature
Milestone
Description
Description
When passing the result of the ConfigurationBinder .Get<T>() directly into a method, I'm still getting AOT/trim warnings.
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>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<PublishAot>true</PublishAot>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="9.0.0-alpha.1.23558.3" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.0-alpha.1.23558.3" />
</ItemGroup>
</Project>using Microsoft.Extensions.Configuration;
IConfigurationSection c = new ConfigurationBuilder().Build().GetSection("Options");
#if WORKS
var o = c.Get<MyOptions>();
Some.Method(o);
#else
Some.Method(c.Get<MyOptions>());
#endif
internal static class Some
{
public static void Method(MyOptions? options) { }
}
public class MyOptions
{
public int MaxRetries { get; set; }
}Expected behavior
I shouldn't get AOT/trim warnings because I'm using the Source Generator.
Actual behavior
1>C:\Users\eerhardt\source\repos\ConsoleApp101\ConsoleApp101\Program.cs(4,13,4,31): warning IL3050: Using member 'Microsoft.Extensions.Configuration.ConfigurationBinder.Get<T>(IConfiguration)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. Binding strongly typed objects to configuration values requires generating dynamic code at runtime, for example instantiating generic types.
1>C:\Users\eerhardt\source\repos\ConsoleApp101\ConsoleApp101\Program.cs(4,13,4,31): warning IL2026: Using member 'Microsoft.Extensions.Configuration.ConfigurationBinder.Get<T>(IConfiguration)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. In case the type is non-primitive, the trimmer cannot statically analyze the object's type so its members may be trimmed.
Regression?
No response
Known Workarounds
Capture the object into a variable on a separate line, and pass that into the method:
var o = c.Get<MyOptions>();
Some.Method(o);Configuration
No response
Other information
No response
Metadata
Metadata
Assignees
Labels
area-Extensions-Configurationbugin-prThere is an active PR which will close this issue when it is mergedsource-generatorIndicates an issue with a source generator feature
Activity
ericstj commentedon Nov 8, 2023
I can reproduce with the latest package from the feed as well. I see the generator is generating code, but the interceptor attribute isn't functioning correctly.
ericstj commentedon Nov 9, 2023
This looks like it's actually a problem with the IL analyzer:
If I disassemble the compiled binary I can see that it's calling the intercepted method that doesn't have the unreferenced code attributes.
ericstj commentedon Nov 9, 2023
Here's a simpler repro that doesn't use the code-generator and demonstrates the bug in the analyzer:
GetTest.zip
In this case I hooked both calls to the method that is annotated - you can see the final binary doesn't call the annotated method, but the warnings are emitted.
eerhardt commentedon Nov 9, 2023
Could it be a bug in here?
runtime/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/ConfigurationBindingGenerator.Suppressor.cs
Lines 20 to 22 in c422fca
I thought this was supposed to suppress the trim/AOT warnings once the call was intercepted.
ericstj commentedon Nov 9, 2023
I think you are right. My simplified repro shows both diagnostics - indicating it might not be possible for IL analyzer to see interceptor at all. The original report only hit for one - indicating we suppressed one but not the other. I wasn't aware of the suppressor, thank you for sharing that.
Bug is likely here:
runtime/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/ConfigurationBindingGenerator.Suppressor.cs
Lines 51 to 57 in c422fca
9 remaining items
rosebyte commentedon Mar 5, 2026
Triage: we will investigate the issue together with other fixes for the generator and potentially fix it.
Fix ConfigurationBinder suppressor to only suppress intercepted calls (…
Fix ConfigurationBinder suppressor to only suppress intercepted calls (…