.NET 8 and Trimming

In .NET 6, trimming worked pretty well, even if your assemblies weren’t annotated properly. In .NET 7 and now with .NET 8, things have changed, and trying to trim with the same project files and settings will cause everything to break.

If you want to get the old .NET 6 behavior, annotate your csproj files with these elements:


  <TrimMode>partial</TrimMode>
  <JsonSerializerIsReflectionEnabledByDefault>true</JsonSerializerIsReflectionEnabledByDefault>

Hoping this saves someone the hours I lost fussing around with this. Microsoft’s trimming guide is good, but doesn’t mention the json property, which is now false by default in .NET 8, instead of true.

5 1 vote
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Rogerio
Rogerio
8 months ago

You are right sr.! It worked for me after 3 days. Microsoft does not mention that.