Skip to content
Latchkey

dotnet publish AOT / Trimming Failures (IL2026, IL3050) in CI

Native AOT (PublishAot) and trimming (PublishTrimmed) statically analyze your code. Reflection or dynamic patterns the analyzer cannot resolve produce IL2026/IL3050 warnings, which publish treats as errors.

What this error means

Publish fails during the AOT/trim step with IL2026 ("requires unreferenced code") or IL3050 ("requires dynamic code") naming the reflective call site. A regular (non-AOT, non-trimmed) build succeeds, which isolates it to the AOT/trim pipeline.

dotnet publish output
error IL2026: Using member 'System.Type.GetMethod(String)' which has
'RequiresUnreferencedCodeAttribute' can break functionality when trimming.
error IL3050: Using member '...MakeGenericType' requires dynamic code generation.

Common causes

Reflection the trimmer/AOT cannot statically resolve

Calls like Type.GetMethod, MakeGenericType, or reflection-based serialization cannot be proven safe at compile time, so the analyzer warns - and publish fails under warnings-as-errors.

A dependency is not AOT/trim compatible

A library that relies on runtime code generation or untrimmable reflection surfaces IL warnings through your publish even if your own code is clean.

How to fix it

Make the reflection statically analyzable

Annotate members with DynamicallyAccessedMembers, use source-generated serialization, or replace reflection with AOT-friendly patterns.

C#
// AOT-friendly System.Text.Json source generation
[JsonSerializable(typeof(MyDto))]
internal partial class AppJsonContext : JsonSerializerContext { }

Confirm dependencies support AOT/trimming

  1. Read each IL2026/IL3050 to find whether it is your code or a dependency.
  2. Prefer AOT-compatible libraries; check the package for trim/AOT support.
  3. Only suppress a specific IL code after verifying the path is genuinely safe.

How to prevent it

  • Adopt source generators (JSON, regex) instead of reflection for AOT/trim builds.
  • Test PublishAot/PublishTrimmed in CI so incompatibilities surface early.
  • Choose dependencies that document AOT and trimming support.

Frequently asked questions

What causes "AOT / trim analysis errors"?
Calls like Type.GetMethod, MakeGenericType, or reflection-based serialization cannot be proven safe at compile time, so the analyzer warns - and publish fails under warnings-as-errors.
How do I fix AOT / trim analysis errors?
Annotate members with DynamicallyAccessedMembers, use source-generated serialization, or replace reflection with AOT-friendly patterns.

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card