This post is a continuation of the “How to publish a NET core application“, the previous post talks about how to use “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. 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.
How to publish a Single Executable (EXE) NET Core application.
NET Core SDK 3.0 and above, at the time of writing the current version, is NET Core v3.0.0-preview7. Download here.
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
- –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.
- –configuration release: The dotnet publish command on receiving this parameter will build the application in release mode.
- PublishSingleFile=true: This PublishSingleFile when set to true, the dotnet publish packages the entire output in a single executable file.
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. To know more about Creating NET Core Console application refer here.
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
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\
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:
- 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.
- 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!!!
Learn how to create an ASP.NET Core MVC application using simple dotnet-cli. or learn how to change Visual Studio 2019 themes.
Learn more about dotnet publish command on Microsoft Developer Network