2

What's the difference between the dotnet build -c Release and dotnet publish commands?

When running dotnet build -c Release and dotnet publish the outputs are placed in /bin/Release/net8.0/ and /bin/Release/net8.0/publish, respectively. Inspecting the contents, it looks identical.

dotnet build defaults to Debug configuration, while dotnet publish defaults to Release.

References: https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-build https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-publish

2
  • 3
    The description of both seems to explain the difference. Specifically the documentation of build states: "With that in mind, the product of dotnet build isn't ready to be transferred to another machine to run. To create a version of the application that can be deployed, you need to publish it (for example, with the dotnet publish command)." (for .NET Core before 3.0), and for later versions: "This means that if there isn't any other publish-specific logic (such as Web projects have), the build output should be deployable." Commented Aug 15, 2024 at 6:06
  • 1
    dotnet build will just compile your code, dotnet publish will pack it into a folder that you can ship to customers. Sometimes the output of build already is enough to ship, sometimes you need to add further steps into your deploy-process. Commented Aug 15, 2024 at 6:07

2 Answers 2

3

dotnet build -c Release is similar to dotnet build, except it builds your application in Release mode, as opposed to Debug mode when you run dotnet build.

dotnet publish also builds your application in Release mode, but it also prepares your .NET application for deployment. This involves additional optimization (one example is trimming where unused code is removed, but it needs to be enabled using PublishTrimming in the csproj), copies all the necessary DLLs, and creates a publish output (publish directory).

dotnet publish can also be used to publish an application for a specific operating system only. In this example, the executable will be a Linux executable instead of the Windows exe:

dotnet publish --runtime linux-x64

You can also use dotnet publish for a self-contained deployment, where the application can run without having .NET installed (e.g. the entire .NET BCL and CLR libraries are copied over):

dotnet publish -c Release --runtime win-x64 --self-contained

Generally, dotnet publish is recommended to use over dotnet build -c Release if you're preparing to release the first or new version of your application.

Sign up to request clarification or add additional context in comments.

2 Comments

"This involves additional optimization" - can you please elaborate on what additional optimizations happen on publish?
You say This involves additional optimization, copies all the necessary DLLs, and creates a publish output (publish directory). In my case, the only difference I observe is the /bin/Release/net8.0/publish directory. I could just have copied /bin/Release/net8.0, which is the same? The optimizations happens because of Release configuration, so they happens both for dotnet build -c Release and dotnet publish?
1

As the command said, the build is not as same as the publish, what you have faced is publish to the local machine, it generated the file is the same, since the publish command will also call the build command. But it support other parameter.

Like below:

1.If the IsPublishable property is set to false for a particular project, the Publish target can't be invoked, and the dotnet publish command only runs the implicit dotnet restore on the project.

<PropertyGroup>
  <IsPublishable>false</IsPublishable>
</PropertyGroup>

2.The publish command support the .pubxml files and some special command and value which the build not supported.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.