Creating Trimmed Self-Contained Single Executable Using .NET Core 3.0

In .NET Core, we have a feature to get the runtime components and dependency components in the same folder where the application executable file resides. With this, the advantage is, when we move this folder to a different system where we don’t have any .NET core installed and ran the executable file with the run without any issue and reason is, the .NET core runtime components reside on the same folder. Following is the dotnet command which helps us to achieve it.
 
dotnet publish -r win-x64 -c Release --self-contained
 
Here, -r represents runtime and -c represents configuration and important part is –self-contained switch which instructs to the compiler to get all the runtime files along with dependencies offline to the Release\netcoreapp3.0\win-x64\publish folder in you application folder path. Here, we are netcoreapp3.0 represents .NET Core 3.0 and this version number may change based on .NET core version. Following is how the folder will look with all the components.
 
Creating Trimmed Self-Contained Single Executable Using .NET Core 3.0
 
This is awesome, but here with these many files where most of them in fact 99% we won’t touch directly except a.exe file and configuration file(s) which may look awkward or there could be a chance of missing any files while moving from one place to another.
 
Just imagine if we have only a single executable file that contains all these files logic? Yes, that’s awesome and it is possible from .NET Core 3.0 with simple settings as follows.
 
dotnet publish -r win-x64 -c Release /p:publishsinglefile=true
 
As observed, we replaced –self-contained switch with publishsinglefile, this command instruct runtime that please make it self contained folder as well merge all the logic into single executable. Now, after executing this command when we observe the same publish folder will look as below.
 
Creating Trimmed Self-Contained Single Executable Using .NET Core 3.0
As we can observe only a single self-contained file created and now it will be easy to move this file from one system to another without the worry of missing any files as well it can run on any system without .NET Core installed as it already contains runtime within itself.
 
But, wait! Here when observe the file size above it showing almost 66MB and this is a basic problem without any logic inside and just imagine the size of the file if you have a huge logic in your application and sometimes it can go to gigabytes. To solve this problem a new option is introduced called publishTrimmed, when we set this option as true, the compiler will compress this file to the maximum extent to decrease the size of the file.
 
dotnet publish -r win-x64 -c Release /p:publishsinglefile=true /p:publishtrimmed=true
 
As observed an extra setting in the above CLI command. Now the file size came to around 25MB without any code changes.
 
Creating Trimmed Self-Contained Single Executable Using .NET Core 3.0
These settings even can be set on project files as follows,
  1. <Project Sdk="Microsoft.NET.Sdk">  
  2.   
  3.   <PropertyGroup>  
  4.     <OutputType>Exe</OutputType>  
  5.     <TargetFramework>netcoreapp3.0</TargetFramework>  
  6.     <PublishSingleFile>true</PublishSingleFile>  
  7.     <PublishTrimmed>true</PublishTrimmed>  
  8.     <RuntimeIdentifier>win-x64</RuntimeIdentifier>  
  9.   </PropertyGroup>  
  10.   
  11. </Project>   
Note
RuntimeIdentifier tag is mandatory if we enable publish single file option.
 
Hope you enjoy this cool feature. Happy Coding!