Make Apps Small Again. Trimming in .NET 5

Make Apps Small Again. Trimming in .NET 5

In .NET 5, this tool goes even deeper, it removes the types and members inside assemblies that are not used by the application, reducing the size even more.

Because .NET Core supports self-contained deployment - runtime and framework libraries are deployed alongside the app, so the end use doesn't have to install .NET runtime. Even though this is a great feature if you want to distribute your app to the end users, it has the downside - the size.

This is why .NET team developed mechanism called assembly trimming. During the publish process ILLink (the tool that does the trimming) "walk" though the code and identify the assemblies that are used by the code and remove the rest from the result bundle.

In .NET 5, this tool goes even deeper, it removes the types and members inside assemblies that are not used by the application, reducing the size even more.

Here is the comparison for the Hello World app (dotnet new console):

Assembly Trimming Result .NET 5Trimming is not safe

The trimming does a static analysis of the code and can only identify types and members when they are statically referenced from the code. But what about reflection? This is a catch. ILink tool will simple remove the assembly, member or type if you do not specify otherwise manually. That is why you should be really careful with enabling trimming and using it in production.

You might think that if you do not use reflection in your code, you are safe, but wait for a moment, there might be some other third party nuget package that rely on reflection. So extensive testing is a must when using trimming.

.net .net core .net 5 trim

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

jQuery Ajax CRUD in ASP.NET Core MVC with Modal Popup

In this article, we’ll discuss how to use jQuery Ajax for ASP.NET Core MVC CRUD Operations using Bootstrap Modal. With jQuery Ajax, we can make HTTP request to controller action methods without reloading the entire page, like a single page application.

MVC User Registration & Login with ASP.NET Core Identity

In this article, we will discuss the quickest way to use ASP.NET Core Identity for User Login and Registration in a new or existing MVC application.

Index is not an attribute class – Error while migrating from ASP.Net MVC 5 to .Net Core

Recently one of my friends was migrating a project from ASP.Net MVC 5 to ASP.Net core 3.1. One of the challenges he faced is with the Index Attribute in data annotations. The .Net Core is not …

Announcing Entity Framework Core (EF Core) 5 RC2

Entity Framework Core EF Core 5.0 gets closer to a final release. RC2 is now available with bug fixes and a go-live license.

Adding Entity Framework Core 5 migrations to .NET 5 project

Database migrations help a developer to keep database schema up-to-date with the code. It is a core mechanism, that keeps changes in code and applies them in the database. Entity Framework Core 5 migrations are designed to keep track of DbContext...