Skip to content

ConfigurationBinder emits AOT warnings even when using Source Generator when passing result into a method #94544

Closed

Description

@eerhardt

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

Activity

  1. ghost added
    untriaged
    on Nov 8, 2023
  2. ericstj commented on Nov 8, 2023

    @ericstj
    Member

    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.

  3. added
    area-Tools-ILLink
    and removed on Nov 9, 2023
  4. ericstj commented on Nov 9, 2023

    @ericstj
    Member

    This looks like it's actually a problem with the IL analyzer:

    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.
    

    If I disassemble the compiled binary I can see that it's calling the intercepted method that doesn't have the unreferenced code attributes.

    private static void <Main>$(string[] args)
    {
    	IConfigurationSection c = new ConfigurationBuilder().Build().GetSection("Options");
    	Some.Method(<BindingExtensions_g>FC276FD7C89D1ED0B2B3E96BAC8F157FABB86A642EAABC54639B357A857C01D08__BindingExtensions.Get<MyOptions>(c));
    }
  5. ericstj commented on Nov 9, 2023

    @ericstj
    Member

    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.

  6. eerhardt commented on Nov 9, 2023

    @eerhardt
    MemberAuthor

    Could it be a bug in here?

    public sealed class Suppressor : DiagnosticSuppressor
    {
    private const string Justification = "The target method has been intercepted by a generated static variant.";

    I thought this was supposed to suppress the trim/AOT warnings once the call was intercepted.

  7. ericstj commented on Nov 9, 2023

    @ericstj
    Member

    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:

    bool shouldSuppressDiagnostic =
    location.SourceTree is SyntaxTree sourceTree &&
    sourceTree.GetRoot().FindNode(location.SourceSpan) is SyntaxNode syntaxNode &&
    BinderInvocation.IsCandidateSyntaxNode(syntaxNode) &&
    context.GetSemanticModel(sourceTree)
    .GetOperation((InvocationExpressionSyntax)syntaxNode, context.CancellationToken) is IInvocationOperation operation &&
    BinderInvocation.IsBindingOperation(operation);

  8. added and removed
    area-Tools-ILLink
    on Nov 9, 2023
  9. added this to the 9.0.0 milestone on Nov 9, 2023
  10. 9 remaining items

  11. removed their assignment
    on Sep 17, 2024
  12. modified the milestones: 9.0.0, 10.0.0 on Sep 17, 2024
  13. modified the milestones: 10.0.0, Future on Jul 26, 2025
  14. rosebyte commented on Mar 5, 2026

    @rosebyte
    Member

    Triage: we will investigate the issue together with other fixes for the generator and potentially fix it.

  15. modified the milestones: Future, 11.0.0 on Mar 5, 2026
  16. self-assigned this
    on Apr 9, 2026
  17. added
    in-pr
    on Apr 14, 2026
  18. locked and limited conversation to collaborators on May 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-Extensions-Configurationbugin-prsource-generator

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions