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.
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.
// AOT-friendly System.Text.Json source generation
[JsonSerializable(typeof(MyDto))]
internal partial class AppJsonContext : JsonSerializerContext { }Confirm dependencies support AOT/trimming
- Read each IL2026/IL3050 to find whether it is your code or a dependency.
- Prefer AOT-compatible libraries; check the package for trim/AOT support.
- 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/PublishTrimmedin CI so incompatibilities surface early. - Choose dependencies that document AOT and trimming support.