Developers who want to distribute their projects quickly must understand how to publish .NET Core applications properly. With the help of the dotnet publish command, you can easily customize the deployment process to meet your requirements. 

You can choose alternative build configurations (such as Release or Debug), target different runtimes, and even create dotnet publish self-contained deployments. With the use of this command, developers may assure compatibility across many environments, optimize application performance, and streamline deployment operations.

We will examine the different options and parameters that the dotnet publish command offers in this article. This will give you the skills to publish your .NET Core apps for various runtime environments efficiently, from simple usage to sophisticated strategies like making self-contained deployments and removing unnecessary assemblies.

First, let’s ensure you have the .NET SDK installed. Open your terminal or command prompt and run:

Code Syntax : dotnet –version

Above code will return the version of the .NET SDK installed on your machine.

  1. Basic dotnet publish Command

To publish a .NET Core application, navigate to the root directory of your project and run:

Code Syntax : dotnet publish

By default, this command:

  • Builds the project in the Release configuration.
  • Publishes the output to the ./bin/Release/net<version>/publish directory.

Example :

This command builds and publishes your application using the default Release configuration to the ./bin/Release/net<version>/publish directory.

  1. Specifying the Output Directory

You can specify a different output directory using the -o or –output option:

Code Syntax : dotnet publish -o ./publish-output
  • This command publishes the application to the ./publish-output directory.

Example : 

Code Syntax : dotnet publish -o /path/to/custom/output
  • Publishes the application to /path/to/custom/output.
  1. Configurations: Release vs Debug

If you need to publish a Debug build, you can specify the -c or –configuration option:

Code Syntax: dotnet publish -c Debug

Example : Debug Configuration

  • Builds and publishes the application in Debug mode.
  1. Target Runtime

You can publish your application for a specific runtime using the -r or –runtime option. For instance, to publish for Windows 64-bit:

Code Syntax: dotnet publish -r win-x64

Example : Specific Runtime

  • Publishes the application for the Windows 64-bit runtime.
  1. Self-Contained Deployment

By default, .NET Core applications are published as framework-dependent deployments, meaning the target machine must have the .NET runtime installed. You can create a self-contained deployment (SCD) that includes the .NET runtime by using the -r option along with –self-contained:

Code Syntax: dotnet publish -r win-x64 --self-contained
  1. Trim Unused Assemblies

For self-contained deployments, you can reduce the size of your application by trimming unused assemblies using the –self-contained and -p:PublishTrimmed=true options:

Code Syntax: dotnet publish -r win-x64 --self-contained -p:PublishTrimmed=true

Example : Trim Unused Assemblies

  • Publishes a trimmed, self-contained application for Windows 64-bit.

Conclusion

To deploy.NET Core applications precisely and effectively, you must become proficient with the dotnet publish command. Gaining knowledge of these capabilities enables you to provide reliable solutions, regardless of whether you need to guarantee compatibility across many platforms or optimize performance with self-contained installations. Think about working with CMARIX if you want professional advice and flawless deployment solutions that are customized to meet your unique needs.

We have a dedicated team of .NET core experts who can assist you with utilizing the dotnet publish command to its fullest. Hire .NET core developers to improve application performance and expedite your deployment procedures.