Create Self Contained Single Exe (Executable) in .NET Core

Create Single exe

Recently I had the requirement for loading multiple DLLs into a single EXE file.

I had to embed multiple DLL resources( both external, third-party vendor, and internal DLL) including system DLL together as a bundle to simplify the usage.

Today in this article, we shall see how to Create Self-Contained Single Exe.

Today in this article, we will cover below aspects,

I was able to achieve the same .NET regular, and .NET Core < 2.2 by means of custom techniques.

From .NET Core 3.0* onwards there is already inbuilt support for creating a self-contained single executable. All you need to change is configuration(no coding).

Depending on your use case Single distributable brings lots of benefits.

In this article, I will talk about an approach to achieve the same using the .NET Core 3.1 or .NET 5 application.

Create a Console app using .NET Core

Let’s build and publish the application using Visual Studio IDE,

Or

Using the command below,


dotnet publish -r win-x64 -c Release 

Either way, it shall produce a deployable result as below depending on your machine and visual studio configuration.

Create Self Contained Single exe

As shown above, I have multiple DLL references which include third-party vendor DLLs and internal DLLs.

Create Self-Contained Single Exe (with Embedded DLL/Resources)

To generate the single executable with one single self-contained file perform the below steps,

Create Single Exe – Using Visual Studio 2019 or VS Code

If using Visual Studio or VS Code IDE, update the project file(.csproj) for PublishSingleFile set to True as below,

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <RuntimeIdentifier>linux-x64</RuntimeIdentifier>
    <PublishSingleFile>true</PublishSingleFile>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\ExternalApp1\ExternalApp1.csproj" />
    <ProjectReference Include="..\ExternalApp2\ExternalApp2.csproj" />
  </ItemGroup>

</Project>

Create Single exe – Using CLI Command

The same above setting can be achieved by using the below CLI command,

For Linux

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

For Windows10

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

OR

For Windows Server

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

Create Self-Contained Single Exe – Using CLI Command

If you need to create a Self-Contained Single Exe then please use the argument ‘–self-contained’ as true.

For Linux

dotnet publish -r linux-x64 -p:PublishSingleFile=true --self-contained true -c Release  

For Windows10

dotnet publish -r win10-x64  -p:PublishSingleFile=true --self-contained true -c Release  

OR

For Windows Server

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

Note: Command ‘-self-contained true‘ will create self-contained large exe with all required runtime binaries related to .NET Core runtime. This also mean your dont need .NET Core runtime or SDK installed on the target server as your exe is now self contained and can run on its own.

Finally – OutPut as EXE

Finally, we got the result as one single EXE of size 76 MB

Create Trimmed Exe – Optimizing size of EXE

If you want to achieve angular-like bundling and minification, please use ‘PublishTrimmed‘ as True in the project file.

We can create trimmed exe by optimizing the size of EXE.

 <PublishTrimmed>true</PublishTrimmed> 

As shown below the file size has been reduced to 44 MB (almost saving of 20 MB memory).

Create Trimmed Exe

Other references :

That’s all. Please let me know your comments below.



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *