How to use the command dotnet publish (with examples)

How to use the command dotnet publish (with examples)

This article will demonstrate various use cases of the dotnet publish command. The dotnet publish command is used to publish a .NET application and its dependencies to a directory for deployment to a hosting system. It provides options to customize the publishing process and optimize the output for specific deployment scenarios.

Use case 1: Compile a .NET project in release mode

Code:

dotnet publish --configuration Release path/to/project_file

Motivation: Compiling a .NET project in release mode ensures that the application is optimized for performance and doesn’t include unnecessary debugging information. This is critical for deploying production-ready applications.

Explanation:

  • --configuration Release: Specifies that the project should be compiled in release mode, enabling optimizations and removing debugging symbols.

Example output:

Microsoft (R) Build Engine version 16.8.0+126527ff1 for .NET
...
...
Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:04.78

Use case 2: Publish the .NET Core runtime with your application for the specified runtime

Code:

dotnet publish --self-contained true --runtime runtime_identifier path/to/project_file

Motivation: When deploying a .NET application to a hosting system, it is sometimes necessary to include the .NET Core runtime with the application. This ensures that the application can run on systems that don’t have the runtime installed.

Explanation:

  • --self-contained true: Specifies that the output should be a self-contained deployment, which includes the .NET Core runtime along with the application.
  • --runtime runtime_identifier: Specifies the target runtime for the published application. The runtime_identifier can be any valid .NET Core runtime identifier.

Example output:

Microsoft (R) Build Engine version 16.8.0+126527ff1 for .NET
...
...
Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:04.79

Use case 3: Package the application into a platform-specific single-file executable

Code:

dotnet publish --runtime runtime_identifier -p:PublishSingleFile=true path/to/project_file

Motivation: Packaging the application into a single-file executable simplifies distribution and reduces the chances of missing or mismatching dependencies during deployment.

Explanation:

  • --runtime runtime_identifier: Specifies the target runtime for the published application. The runtime_identifier can be any valid .NET Core runtime identifier.
  • -p:PublishSingleFile=true: Enables the option to package the application as a single-file executable.

Example output:

Microsoft (R) Build Engine version 16.8.0+126527ff1 for .NET
...
...
Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:05.00

Use case 4: Trim unused libraries to reduce the deployment size of an application

Code:

dotnet publish --self-contained true --runtime runtime_identifier -p:PublishTrimmed=true path/to/project_file

Motivation: Removing unused libraries from the deployment package reduces the overall size of the application, making the deployment process faster and more efficient.

Explanation:

  • --self-contained true: Specifies that the output should be a self-contained deployment, which includes the .NET Core runtime along with the application.
  • --runtime runtime_identifier: Specifies the target runtime for the published application. The runtime_identifier can be any valid .NET Core runtime identifier.
  • -p:PublishTrimmed=true: Enables trimming of unused libraries from the deployment package.

Example output:

Microsoft (R) Build Engine version 16.8.0+126527ff1 for .NET
...
...
Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:04.80

Use case 5: Compile a .NET project without restoring dependencies

Code:

dotnet publish --no-restore path/to/project_file

Motivation: In certain scenarios, you may have already restored the dependencies of the project and don’t want to repeat the process during the publish command. This can save time, especially when publishing large projects.

Explanation:

  • --no-restore: Skips the restore step and uses the existing dependencies to compile the project.

Example output:

Microsoft (R) Build Engine version 16.8.0+126527ff1 for .NET
...
...
Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:04.80

Use case 6: Specify the output directory

Code:

dotnet publish --output path/to/directory path/to/project_file

Motivation: By default, the dotnet publish command outputs the published application in the bin/Debug/netcoreappX.X/publish directory. Specifying the output directory allows you to control where the published files are stored.

Explanation:

  • --output path/to/directory: Specifies the output directory where the published files should be placed.

Example output:

Microsoft (R) Build Engine version 16.8.0+126527ff1 for .NET
...
...
Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:04.78

Conclusion:

The dotnet publish command is a powerful tool for publishing .NET applications and customizing the publishing process. It allows you to optimize the output for different deployment scenarios, including compiling in release mode, packaging as a single-file executable, including the .NET Core runtime, trimming unused libraries, and specifying the output directory. Understanding and utilizing these different use cases will help you effectively deploy your .NET applications.

Related Posts

How to use the command 'git var' (with examples)

How to use the command 'git var' (with examples)

Git var is a command included in the Git version control system that allows users to print the value of a Git logical variable.

Read More
How to use the command 'pinout' (with examples)

How to use the command 'pinout' (with examples)

The command ‘pinout’ is a useful tool for Raspberry Pi users to view the pinout information and GPIO header diagram for their current Raspberry Pi.

Read More
How to Use the GIMP Command Line Interface (with examples)

How to Use the GIMP Command Line Interface (with examples)

1: Starting GIMP gimp Motivation: Starting GIMP using the gimp command opens the program’s graphical user interface (GUI), allowing users to create and edit images.

Read More