Skip to content

[Bug]: NativeAOT / trimming warnings in Refit 11.0.1 #2121

Description

@RoccoZero

Describe the bug 🐞

Hi.

Can you please check and fix the NativeAOT / trimming warnings in Refit?

When publishing my project with NativeAOT enabled, I get many IL2026, IL2104, and IL3053 warnings from Refit and Refit.HttpClientFactory.

I would be very grateful if these warnings could be fixed or reduced, because my project targets NativeAOT and these warnings are very annoying/noisy during build.

The main warning comes from:

Refit.IRequestBuilder.BuildRestResultFuncForMethod(String, Type[], Type[])

It is marked with RequiresUnreferencedCodeAttribute, and the generated Refit implementation calls it, which produces many warnings.

Example warnings:

ILC : Trim analysis warning IL2026: Refit.Implementation.Generated.DivineClientRestIAuthRestApi.<SignIn>d__6.MoveNext(): Using member 'Refit.IRequestBuilder.BuildRestResultFuncForMethod(String,Type[],Type[])' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Refit uses reflection to analyze interface methods. Ensure referenced interfaces and DTOs are preserved when trimming.

Refit.HttpClientFactory.dll : warning IL2104: Assembly 'Refit.HttpClientFactory' produced trim warnings.

Refit.dll : warning IL2104: Assembly 'Refit' produced trim warnings.

Refit.dll : warning IL3053: Assembly 'Refit' produced AOT analysis warnings.

SystemTextJsonContentSerializer.cs: Trim analysis warning IL2026: JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext.

Step to reproduce

I am using:

  • Refit: 11.0.1
  • Refit.HttpClientFactory: 11.0.1
  • Target framework: net10.0
  • NativeAOT enabled
  1. Create a .NET project that uses Refit.
  2. Enable NativeAOT / trimming.
  3. Add Refit interfaces and use source-generated Refit implementation.
  4. Publish/build the project.
  5. See NativeAOT / trimming warnings.

Reproduction repository

https://github.com/reactiveui/refit

Expected behavior

Refit should build cleanly with NativeAOT/trimming enabled, or provide an official NativeAOT-friendly way to avoid these warnings.

Ideally, source-generated Refit clients should not call APIs marked with RequiresUnreferencedCodeAttribute, such as:

Refit.IRequestBuilder.BuildRestResultFuncForMethod(String, Type[], Type[])

Also, JSON serialization should use JsonSerializerContext / JsonTypeInfo-based overloads where possible, so NativeAOT projects can avoid IL2026, IL2104, and IL3053 warnings.

Screenshots 🖼️

No response

IDE

Visual Studio 2022

Operating system

Windows 11

Version

No response

Device

No response

Refit Version

11.0.1

Additional information ℹ️

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions