How to Publish Single Executable in NET Core using dotnet-cli

This post talks about how to publish single executable NET Core application, which is NET Core framework runtime independent and the user need not require it to install them before executing the published application.

The post is a continuation of the “How to publish a NET core application“, the previous post talks about how to use the “dotnet publish” command and to tweak the output of the command using various options available with the same. It is recommended to read through to gain a better understanding of the “dotnet publish” command.

How to publish a Single Executable – EXE

Let us first understand the dotnet publish command syntax and the runtime identifiers that can be used along with it. Then we will build a simple console application and publish it as a single executable.

Prerequisite:

NET Core SDK 3.0 and above, at the time of writing the current version, is NET Core 5.0 and can be downloaded here.

Command Syntax – dotnet publish:

In order to publish single executable NET Core application, we are going to use the dotnet publish command with the given parameters.

dotnet publish –runtime <RID> –configuration release /p:PublishSingleFile=true

  1. –runtime <RID>: The runtime switch accepts a RID known as Runtime Identifiers, these RID is used to identify the target platforms where the application will be executed. The list of RID supported can be found here. Few runtime identifiers are win10-x64, linux-x64, linux-arm, osx.10.10-x64.
  2. –configuration release: The dotnet publish command on receiving this parameter will build the application in release mode.
  3. PublishSingleFile=true: This PublishSingleFile when set to true, the dotnet publish packages the entire output in a single executable file.

Create a .NET Core console application and publish

Here in this example we will build a NET Core console application and publish the same as a single executable file. The name of the console application is Hello and we will use the following command to create the application. The command will create a new directory named Hello if already not present and then create the Hello Console project in it. Please refer to know more about Creating NET Core Console application.

dotnet new console –name Hello

Now we will publish the console application as Single Executable targeting the Windows x64 system. (You can use any Runtime Identifiers (RID) for experimenting) The command is as follows:

dotnet publish –configuration release –runtime win-x64 /p:PublishSingleFile=true

dotnet publish single executable

Command Output:

c:\Hello>dotnet publish --runtime win-x64 --configuration release /p:PublishSingleFile=true
Microsoft (R) Build Engine version 16.2.0-preview-19278-01+d635043bd for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Restore completed in 21.27 ms for c:\Hello\Hello.csproj.
C:\Program Files\dotnet\sdk\3.0.100-preview6-012264\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.RuntimeIdentifierInference.targets(158,5): message NETSDK1057: You are using a preview version of .NET Core. See: https://aka.ms/dotnet-core-preview [c:\Hello\Hello.csproj]
  Hello -> c:\Hello\bin\release\netcoreapp3.0\win-x64\Hello.dll
  Hello -> c:\Hello\bin\release\netcoreapp3.0\win-x64\publish\

Artifacts Generated:

c:\Hello\bin\release\netcoreapp3.0\win-x64\publish>dir
 Volume in drive C is OSDisk
 Volume Serial Number is 8053-FAC8

 Directory of c:\Hello\bin\release\netcoreapp3.0\win-x64\publish

07/25/2019  08:09 PM    <DIR>          .
07/25/2019  08:09 PM    <DIR>          ..
07/25/2019  08:09 PM        72,310,555 Hello.exe
07/25/2019  08:02 PM               404 Hello.pdb

Here we have only two files:

  1. Hello.exe: This is the single executable that is generated and can be executed on any Windows x64 system without having the dotnet core runtime installed.
  2. Hello.pdb: This is the project debug file and is useful for debugging purposes.

The user can take this Hello.exe file and can execute it on any Windows x64 system without worrying about the NET Core runtime availability. The size of this single executable is quite large as it packages all the required dependencies to execute itself, and when the application is executed for the first time, it extracts all the dependencies first and then executes itself, so it startup is little expensive only for the first time.

This concludes the article on how to publish single executable NET Core application, I hope you find it helpful, thanks for visiting. Cheer!!!

[Further Readings: How to Publish a NET Core application |  How to change Visual Studio 2019 Theme |  How to create an ASP NET Core MVC Web Application using dotnet-cli |  How to create an ASP.NET Core Web Application using dotnet-cli |  How to create a dotnet core NUnit Test Project using dotnet-cli |  How to create a dotnet core xUnit Test Project using dotnet-cli |  How to create a dotnet core MSTest Project using dotnet-cli |  How to create dotnet core WinForms Application using dotnet-cli |  How to create a dotnet core WPF application using dotnet-cli |  How to create a dotnet core console app using dotnet-cli |  Top 7 Web Frameworks to Learn and Focus on in 2021   |  Singleton Design Pattern in C#  ]

5 1 vote
Article Rating
Subscribe
Notify of
guest
19 Comments
oldest
newest most voted
Inline Feedbacks
View all comments
19
0
Would love your thoughts, please comment.x
()
x