Overview
Assembly trimming removes unused parts of libraries from the application package, thereby reducing size.
Trimming is only available for self-contained deployments.
Considerations
Enabling
To enable trimming:
- Enable trimming in the project file:
SomeProject.csproj
<PropertyGroup> <PublishTrimmed>true</PublishTrimmed> </PropertyGroup>
- Publish the app as self-contained
- Via CLI:
Note: Thedotnet publish -r <RUNTIME-ID>
-p:PublishTrimmed=true
switch is not required if trimming is enabled in the project file.- Via Visual Studio:
- Right-click project in Solution Explorer > Publish
- More actions > Edit
- Profile settings dialog > Set the Deployment mode > Set the Target runtime > Check Trim unused code > Save
- Publish
Trimming Options
The following MSBuild properties and items change trimming behavior.
Note: ILLink is the name of the underlying tool that implements trimming.
Trim Mode
The default trim mode in .NET 7 is full
. This trims all assemblies:
<TrimMode>partial</TrimMode>
Set trimmable assemblies
If the above is set to partial
, set this to opt-in individual assemblies:
<ItemGroup>
<TrimmableAssembly Include="ASSEMBLY-NAME" />
</ItemGroup>
Root assemblies
Assemblies that are not trimmed is considered “rooted.” Additional assemblies can be rooted:
<ItemGroup>
<TrimmerRootAssembly Include="ASSEMBLY-NAME" />
</ItemGroup>
Analysis warnings
SuppressTrimAnalysisWarnings
is boolean if trim analysis warnings should be enabled. Default =false
.EnableTrimAnalyzer
is boolean to enable a limited set of Roslyn analyzers. Default =true
.ILLinkTreatWarningsAsErrors
is boolean to treat warnings as errors.TrimmerSingleWarn
is boolean to show detailed errors instead of collapsing them into a single warning per assembly.TrimmerRemoveSymbols
is boolean to remove symbols (such as PDBs) from the trimmed application.
Framework Features Automatically Disabled When Trimming
Preparing a Library for Trimming
See https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/prepare-libraries-for-trimming