Skip to main content
Enchanted Code

Publishing With dotnet CLI

tutorial, cs

Intro

Just a small tutorial on how to publish a dotnet app.

Requirements

  • .NET5 or .NET6
  • dotnet CLI

Why Use Publish?

We would use the publish command to create a “release version” of our app for users to run. This is because giving someone a copy of the debug version could increase security risks and produce a slower program. Most users also wont have the .NET SDK to run the app.

How

First you need to open a terminal in your project location.

To publish your first app, using the default config which will most likely be DEBUG.

dotnet publish

Once that is done if you look in the bin/Debug/publish folder of your project you will see that there are some files one of them including a executable version of your app, this will have the same name as your project. Also among the files you will see <project name>.pdb, this pdb file can be used for debugging your app.

Debugging your app using the pdb files is not discussed in this tutorial

Publishing a debug version of your app is not really what you want to release to the public, so let’s now move on to using the “Release” config.

Change Config Type

We can change the config to use using the “-c” flag.

dotnet publish -c Release

When this is run we get a release version of our app. This time navigate to: bin/Release/publish. You should see the same files as the debug version including the “.pdb” files.

Including Runtime

So far we have published a app that requires we have the “.NET runtime” installed, however we may want to bundle it into our app so the user does not need to install it.

We can do this using the “–self-contained” flag.

dotnet publish -c Release --self-contained true

If we look back in the publish directory you will see the entire “.NET runtime”, this however has its downsides it will require more space on a users computer.

Create A Single File

Now we look at publishing everything into a single executable, which is much more portable and neater as the user will only need to copy a single file.

dotnet publish -c Release -p:PublishSingleFile=true

However this still leaves some files out in the open. We can fix this by specifying a new flag.

dotnet publish -c Release -p:PublishSingleFile=true -p:IncludeNativeLibrariesForSelfExtract=true

This will add the native libraries into the executable, they will however be extracted on running the application.

ReadyToRun (R2R)

We can speed up the loading performance of our application by compiling assemblies that are needed. This however has a detrimental impact on the amount of space required and could slow the loading time down if reading from disk.

To use this flag we also have to specify what platform to compile for. We do this with the “-r” flag, in this command I have selected Windows 64 bit.

dotnet publish -c Release -r win-x64 -p:PublishReadyToRun=true

Trim

After producing a large published folder we can use a new trim command available in .NET 6. This can reduce the output, however it doesn’t work for every project as there incompatibilities. We must also make sure to use self-contained flag.

dotnet publish -c Release --self-contained true -p:PublishTrimmed=true

What I Use

Here is what I use when generating a binary of my apps. I always bundle the runtime as most people have plenty of storage and I like to use the latest .NET versions which are not pre-installed on Windows.

dotnet publish -c Release -r win-x64 --self-contained true -p:PublishSingleFile=true -p:IncludeNativeLibrariesForSelfExtract=true

References