What is the Microsoft .NET IL Linker?
Posted: (EET/GMT+2)
With .NET Core 2.0, Microsoft introduced the IL Linker, a tool that analyzes managed assemblies and removes unused code. The idea is simple: when you publish your application, only the parts of the framework that you actually use are kept in the final output.
This can significantly reduce application size, especially for self-contained deployments or when targeting smaller environments like containers. The linker works by walking through your code, following references, and trimming out everything that's never called.
You can enable it in your project file by setting:
<PropertyGroup> <PublishTrimmed>true</PublishTrimmed> </PropertyGroup>
When you publish with "dotnet publish", the IL Linker runs automatically. The output contains only the code paths your application needs, which can make a surprisingly big difference. In some cases cutting deployment size in half.
Be cautious, though: aggressive trimming can remove code that is used via reflection or dynamically loaded at runtime. If you use frameworks that rely on reflection, you may need to add linker configuration files to keep those members intact.