Publish a NET Core application

NET core command-line interface (dotnet-cli) is a new NET Core toolset (commands) for developing NET Core Applications. The dotnet-cli toolset provides a list of commands to create, build, execute and publish a NET Core application. The NET Core SDK comes with pre-installed project templates from which the user can create various applications as a Console app, Web application, WPF application, Windows Form application, and other test applications. The application created with dotnet-cli can be executed on any Windows, Linux or macOS operating system. The NET Core can be downloaded here.

How to publish a NET Core application using “dotnet-cli publish” Command.

The user can use the “dotnet publish” command to publish a NET Core application, the command provides few options using which the user can tweak the output of the command. For example, the dotnet publish –output “c:\output” command output the published artifacts into c:\output directory. Please follow through to read about few “dotnet publish” command usage.

Prerequisite:

NET Core SDK 1.x and above

The syntax of the command is as follows: dotnet publish [options]

The details of all the available command options can be listed using the —help option as follows:

c:\>dotnet publish --help
Usage: dotnet publish [options] <PROJECT | SOLUTION>

Arguments:
  <PROJECT | SOLUTION>   The project or solution file to operate on. If a file is not specified, the command will search the current directory for one.

Options:
  -h, --help                            Show command line help.
  -o, --output <OUTPUT_DIR>             The output directory to place the published artifacts in.
  -f, --framework <FRAMEWORK>           The target framework to publish for. The target framework has to be specified in the project file.
  -r, --runtime <RUNTIME_IDENTIFIER>    The target runtime to publish for. This is used when creating a self-contained deployment.
                                        The default is to publish a framework-dependent application.
  -c, --configuration <CONFIGURATION>   The configuration to publish for. The default for most projects is 'Debug'.
  --version-suffix <VERSION_SUFFIX>     Set the value of the $(VersionSuffix) property to use when building the project.
  --manifest <MANIFEST>                 The path to a target manifest file that contains the list of packages to be excluded from the publish step.
  --no-build                            Do not build the project before publishing. Implies --no-restore.
  --self-contained                      Publish the .NET Core runtime with your application so the runtime doesn't need to be installed on the target machine.
                                        The default is 'true' if a runtime identifier is specified.
  /nologo, --nologo                     Do not display the startup banner or the copyright message.
  --interactive                         Allows the command to stop and wait for user input or action (for example to complete authentication).
  --no-restore                          Do not restore the project before building.
  -v, --verbosity <LEVEL>               Set the MSBuild verbosity level. Allowed values are q[uiet], m[inimal], n[ormal], d[etailed], and diag[nostic].
  --no-dependencies                     Do not restore project-to-project references and only restore the specified project.
  --force                               Force all dependencies to be resolved even if the last restore was successful.
                                        This is equivalent to deleting project.assets.json.

Here we will be using a NET Core Console application to demonstrate the use of the dotnet publish command and understand the artifacts generated by it. The first step is the create a NET Core console application, we do the same using a dotnet new console command. We will create a console program named Hello as given below and execute the same.

Step 1: Create a NET Core Console application and execute it, as shown below.

Execute the commands:

  • dotnet new console –name Hello
  • dotnet run

The command creates a folder name Hello and creates the console application in it, the second command builds and executes the commands, the output of both the commands are given below.

how to publish a NET Core application, dotnet publish
1. Creating Hello Console Application
dotnet publish
2. Executing Application

Step 2: Publish the Hello console application

To publish the console application we will use the dotnet publish command, the command first compiles the application, then read through the dependencies required by the applications and then creates the publish folder inside the /debug/netcoreversion/publish. The published version of the console application is framework dependent and would required the NET Core Runtime to be installed before executing the application. The output of the publish command is given below, we will go through every file to understand more.

dotnet publish
Executing: dotnet publish command

The output of the command

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

 Directory of c:\Hello\bin\Debug\netcoreapp3.0\publish

07/19/2019  03:05 PM    <DIR>          .
07/19/2019  03:05 PM    <DIR>          ..
07/19/2019  02:36 PM               407 Hello.deps.json
07/18/2019  08:08 PM             4,608 Hello.dll
07/18/2019  08:08 PM           159,744 Hello.exe
07/18/2019  08:08 PM               408 Hello.pdb
07/19/2019  02:36 PM               172 Hello.runtimeconfig.json

Followings are the artifacts generated by the publish command

  1. Hello.deps.json: Include details about the dependencies of the project.
  2. Hello.dll: This is the framework dependent executable file containing the Intermediate Langauge (IL) having a .dll extension.
  3. Hello.exe: This is the executable file generated on Windows Machine only.
  4. Hello.pub: This is the Project Debug File created when the dotnet publish command is used with debug configuration which is the default mode.
  5. Hello.runtimeconfig.json: This file contains details about the runtime the application requires in order to execute.

The content of the Hello.deps.json and Hello.runtimeconfig.json is given below, the content might differ are per your dotnet cli version, mine is v3.0.0-preview6

c:\Hello\bin\Debug\netcoreapp3.0\publish>type Hello.deps.json
{
  "runtimeTarget": {
    "name": ".NETCoreApp,Version=v3.0",
    "signature": ""
  },
  "compilationOptions": {},
  "targets": {
    ".NETCoreApp,Version=v3.0": {
      "Hello/1.0.0": {
        "runtime": {
          "Hello.dll": {}
        }
      }
    }
  },
  "libraries": {
    "Hello/1.0.0": {
      "type": "project",
      "serviceable": false,
      "sha512": ""
    }
  }
}
c:\Hello\bin\Debug\netcoreapp3.0\publish>type Hello.runtimeconfig.json
{
  "runtimeOptions": {
    "tfm": "netcoreapp3.0",
    "framework": {
      "name": "Microsoft.NETCore.App",
      "version": "3.0.0-preview6-27804-01"
    }
  }
}

A good read: How to create NET Core WPF (Windows Presentation Foundation) app.

Various command options to tweak the output

dotnet publish –configuration release: the command is used to publish the application in Release Mode. The command will create the artifacts in \release\netcoreversion\publish folder. dotnet publish -c release short version can also be used.

dotnet publish –framework <targetframeworkversion>: The dotnet publish command can be used to target a specific version of the NET framework, the command can also be used to multiple framework targets at the same time. The list of the framework that is currently supported can be found here

Target single framework: Here in this example we are going to publish the console application targetting NET Core version 2.2, please note the console application created is in NET Core version 3.0 preview-6. For this, we are going to modify the Hello.csproj and add netcoreapp2.2 in TargetFramework, and rename TargetFramework to TargetFrameworks.

c:\Hello>type Hello.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFrameworks>netcoreapp3.0;netcoreapp2.2</TargetFrameworks>
  </PropertyGroup> 
</Project>

Now we are going to execute the following command: dotnet publish –framework netcoreapp2.2, dotnet publish -f netcoreapp2.2 short version can also be used

c:\Hello>dotnet publish --framework netcoreapp2.2
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 24.26 ms for c:\Hello\Hello.csproj.
[c:\Hello\Hello.csproj]
  Hello -> c:\Hello\bin\Debug\netcoreapp2.2\Hello.dll
  Hello -> c:\Hello\bin\Debug\netcoreapp2.2\publish\

Publishing Self-Contained Framework Independent NET Core Application

The NET Core application can be published as Framework Independent package where the application need not require NET Core runtime to be installed on the target system. In order to publish the application, the user needs to specify the runtime environment for which the application is to be published and these runtimes are known as “Runtime Identifier”, the list of supported runtime identifier can be found here. Here in this example, we are going to publish the console application targeting Linux-x64. The command syntax is: dotnet publish –runtime <Runtime Indentifier> –self-contained true

Now we are going to execute the following command: dotnet publish –framework netcoreapp2.2 –runtime linux-x64 –self-contained true. The published artifacts are ready to be executed on any Linux-x64 architecture and the runtime need not be present in advance.

c:\Hello>dotnet publish --framework netcoreapp2.2 --runtime linux-x64 --self-contained 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 59.06 sec 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\Debug\netcoreapp2.2\linux-x64\Hello.dll
  Hello -> c:\Hello\bin\Debug\netcoreapp2.2\linux-x64\publish\

The content of the published folder are as follows:

c:\Hello\bin\Debug\netcoreapp2.2\linux-x64\publish>dir
 Volume in drive C is OSDisk
 Volume Serial Number is 8053-FAC8

 Directory of c:\Hello\bin\Debug\netcoreapp2.2\linux-x64\publish

07/19/2019  07:43 PM    <DIR>          .
07/19/2019  07:43 PM    <DIR>          ..
04/17/2019  09:27 PM           130,368 createdump
07/19/2019  07:43 PM           106,912 Hello
07/19/2019  07:43 PM            36,942 Hello.deps.json
07/19/2019  07:43 PM             4,096 Hello.dll
07/19/2019  07:43 PM               408 Hello.pdb
07/19/2019  07:43 PM                28 Hello.runtimeconfig.json
04/17/2019  09:27 PM         3,126,600 libclrjit.so
04/17/2019  09:27 PM        10,297,824 libcoreclr.so
04/17/2019  09:27 PM           718,384 libcoreclrtraceptprovider.so
04/17/2019  09:27 PM         1,076,400 libdbgshim.so
04/18/2019  01:37 AM           692,128 libhostfxr.so
-----------------------------------
04/18/2019  01:39 AM            13,312 System.Xml.dll
04/18/2019  01:39 AM             4,608 System.Xml.Linq.dll
04/18/2019  01:39 AM            11,776 System.Xml.ReaderWriter.dll
04/18/2019  01:39 AM             5,120 System.Xml.Serialization.dll
04/18/2019  01:39 AM             5,120 System.Xml.XDocument.dll
04/18/2019  01:39 AM             5,120 System.Xml.XmlDocument.dll
04/18/2019  01:39 AM             7,168 System.Xml.XmlSerializer.dll
04/18/2019  01:39 AM             5,120 System.Xml.XPath.dll
04/18/2019  01:39 AM             6,144 System.Xml.XPath.XDocument.dll
04/18/2019  01:39 AM             4,608 WindowsBase.dll

The list is huge as it is self-contained and does not require any pre-installed NET Core runtime dependencies. So I have removed some of the files from the display.

I hope the article helped you to understand about how to publish a NET Core application, thanks for visiting. Cheers!!!

Read about: How to change Visual Studio 2019 Theme or How to create an ASP.NET Core MVC application.

Please read about dotnet publish command on MSDN

0 0 vote
Article Rating
Subscribe
Notify of
guest
2 Comments
oldest
newest most voted
Inline Feedbacks
View all comments