Breaking change for trimming in .NET 7 Preview 7 #30401
Labels
breaking-change
Indicates a .NET Core breaking change
🏁 Release: .NET 7
Work items for the .NET 7 release
Breaking change notice
In .NET 7, trimming will trim all assemblies in the app by default. This breaking change only affects apps which are published with
PublishTrimmed=true
, and it only affects apps with trim warnings.Description
This is a behavior change for console apps (i.e., plain .NET core apps that don't use WindowsDesktop, Android, iOS, WASM, or ASP.NET SDKs). The change is that trimming will now trim all the assemblies in the app by default, as opposed to the pre-.NET 7 behavior, which only trimmed assemblies that had opted-in with
<IsTrimmable>true</IsTrimmable>
in the library project file.Effects
Apps which may have previously worked with
PublishTrimmed
may not in .NET 7. Only apps with trim warnings may be affected. If your app has no trim warnings, the change in behavior should not cause any adverse affects, and will likely decrease the app size.Example
An app which uses
Newtonsoft.Json
orSystem.Text.Json
without source generation to serialize and deserialize a type in the user project may function before the change because types in the user project were fully preserved. One or more trim warnings (warning codesILxxxx
) would have been present.After the change, types in the user project will be trimmed and serialization may fail or produce unexpected results.
Fixes
The best fix is to resolve all trim warnings in your application. For your own libraries, that means resolving the warnings yourself (see https://docs.microsoft.com/en-us/dotnet/core/deploying/trimming/fixing-warnings for more information). For other libraries, you may contact the author to request that they resolve the warnings, or you may choose a different library which already supports trimming.
For instance, instead of
Newtonsoft.Json
you could chooseSystem.Text.Json
with source generation, which supports trimming.Workaround
To go back to the previous behavior, use the
TrimMode
property. The pre-.NET 7 behavior isThe .NET 7+ behavior is
The text was updated successfully, but these errors were encountered: