.NET 7 Self-Contained, NativeAOT, and ReadyToRun Cheatsheet
Cheatsheet for self-contained, trimming, single file, Native AOT, and ReadyToRun for project files.

Note that not all flags might be effective in your specific situation. Make sure you:
- Check if a flag is doing anything for your codebase
- Check your code still works after setting up your set of flags
The following can be dumped into your .csproj
files.
Self-contained
<PropertyGroup>
<PublishSingleFile>true</PublishSingleFile>
<SelfContained>true</SelfContained>
<PublishTrimmed>true</PublishTrimmed>
<InvariantGlobalization>true</InvariantGlobalization>
<EnableCompressionInSingleFile>true</EnableCompressionInSingleFile>
<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
<DebugType>none</DebugType>
<DebugSymbols>false</DebugSymbols>
</PropertyGroup>
AOT
<!-- Normal .NET 7 -->
<PublishAot>true</PublishAot>
<SelfContained>true</SelfContained>
<TrimmerDefaultAction>link</TrimmerDefaultAction>
<InvariantGlobalization>true</InvariantGlobalization>
<IlcGenerateStackTraceData>false</IlcGenerateStackTraceData>
<IlcOptimizationPreference>Size</IlcOptimizationPreference>
<DebugType>none</DebugType>
<GenerateRuntimeConfigurationFiles>false</GenerateRuntimeConfigurationFiles>
<!-- Only some may work - From the experimental AOT version -->
<IlcFoldIdenticalMethodBodies>true</IlcFoldIdenticalMethodBodies>
<IlcTrimMetadata>true</IlcTrimMetadata>
<IlcInvariantGlobalization>true</IlcInvariantGlobalization>
<IlcGenerateStackTraceData>false</IlcGenerateStackTraceData>
<IlcGenerateCompleteTypeMetadata>false</IlcGenerateCompleteTypeMetadata>
<!-- Still works from the experimental AOT version, but high risk -->
<!-- <IlcDisableReflection>true</IlcDisableReflection> -->
ReadyToRun
<PublishReadyToRunComposite>true</PublishReadyToRunComposite>
<SelfContained>true</SelfContained>
<PublishSingleFile>true</PublishSingleFile>
<PublishTrimmed>true</PublishTrimmed>
<DebugType>none</DebugType>
<EnableCompressionInSingleFile>true</EnableCompressionInSingleFile>
<InvariantGlobalization>true</InvariantGlobalization>
References
- Trimming Documentation
- Single File Deployment Documentation
- NativeAOT GitHub Optimisation Documentation
- Experimental AOT GitHub Optimisation Documentation
- Reflection free NativeAOT GitHub Documentation
- ReadyToRun Documentation