A self-contained app is a great way to share your application as all components, runtime, and framework are included with the application. All you have to provide is the application .exe file without worrying about the presence of framework or runtime installation status on other machines. .NET Core 3.0 Preview 6 is out and there are a lot more new features. One of the useful feature is the introduction of PublishTrimmed flag. This flag reduces the size of the executable to a great extent and create a trimmed self-contained single executable in .NET Core 3.0.



Create a Trimmed Self-Contained Single Executable in .NET Core 3.0

Today with .NET Core, you can create a self-contained application using the following command.

dotnet publish -r win-x64 -c Release --self-contained

The above command builds the app in release mode and publishes the self-contained app. This will create a folder with the application .exe and other dependencies. So to run this app on another machine, you have to copy the complete folder and run the .exe from there. This is good, but there are more than 100 files and the size is also huge for a simple .NET Core based console application.

This problem is resolved in .NET Core 3.0 Preview 5. This release introduces a flag called PublishSingleFile which produces a single .exe file. The command can be used in the following way,

dotnet publish -r win-x64 -c Release /p:PublishSingleFile=true

OR, you can also specify in the project file.

<PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>netcoreapp3.0</TargetFramework> <RootNamespace>Hello_.NETCore3</RootNamespace> <PublishSingleFile>true</PublishSingleFile> <RuntimeIdentifier>win-x64</RuntimeIdentifier> </PropertyGroup>

The PublishSingleFile flag produces the following output. As you can see from the below image, there is only a single .exe file now.

A single .exe now, but the size is still an issue. It is near to 70 MB. This is solved in Preview 6 of .NET Core 3.0 with the introduction of a new flag called PublishTrimmed which doesn’t include DLL’s that aren’t used to reduced the size. You need to use both the flags together to create a Trimmed Self-Contained Single Executable in .NET Core 3.0. Like,

dotnet publish -r win-x64 -c Release /p:PublishSingleFile=true /p:PublishTrimmed=true

OR, you can also specify both the flags in the project file.

<PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>netcoreapp3.0</TargetFramework> <RootNamespace>Hello_.NETCore3</RootNamespace> <PublishSingleFile>true</PublishSingleFile> <PublishTrimmed>true</PublishTrimmed> <RuntimeIdentifier>win-x64</RuntimeIdentifier> </PropertyGroup>

And the result is just a 29 MB single EXE. You should be saying that it is still quite big for a simple console application. But remember, it’s a self-contained application which includes the .NET Core runtime also.

That’s it.

Thank you for reading. Keep visiting this blog and share this in your network. Please put your thoughts and feedback in the comments section.

PS: If you found this content valuable and want to return the favour, then If you found this content valuable and want to return the favour, then