dotnet-daily-tips

042 - Publish trimmed

Yesterday, information about publishing options were shared. Today - and update to one of them.

With the self-contained application we have an option to reduce the output file size. Trimming (or tree-shaking) is a technique to remove unused part for the deployed binaries so that they only contain whatโ€™s needed for the application to run. Thus making the deployment smaller ๐Ÿ—œ.

This is an automated process so there is a chance that the analysis will yield wrong results so caution is required. We do get warnings during the build process if the trimmer canโ€™t analyze fully our code pattern.

To enable trimming we need to set PublishTrimmed to true in the project file. After that publish self-contained application.

Docs ๐Ÿ“‘: https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/trim-self-contained

<PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net9.0</TargetFramework>
    <ImplicitUsing>enable</ImplicitUsing>
    <Nullable>enable</Nullable>

    <!-- add this ๐Ÿ‘‡ -->
    <PublishTrimmed>true</PublishTrimmed>
</PropertyGroup>