skip to content

With the .NET 10 SDK, how do dotnet publish -r, --self-contained, PublishSingleFile and PublishReadyToRun change what you ship for a command-line tool?

level: middleimportance: must knowfreq 60%

answer

  1. framework-dependent is the default
  2. RID no longer implies self-contained (net8.0+)
  3. --sc bundles the runtime
  4. single file: one apphost bundle
  5. ReadyToRun: bigger files, faster startup

basics

~10 s

-r picks the target OS and CPU, --self-contained bundles the .NET runtime, PublishSingleFile packs the app into one executable, and PublishReadyToRun precompiles assemblies for faster startup; on net8.0+ a runtime identifier alone stays framework-dependent.

solid answer

~40 s

By default `dotnet publish` produces a **framework-dependent** app: the target machine needs the matching .NET runtime. `-r linux-x64` makes the output **RID-specific** (native apphost, platform assets) in `bin/Release/net10.0/linux-x64/publish/`, but since `net8.0` it no longer implies self-contained. `--self-contained` (`--sc`) copies the runtime in, so nothing must be installed, at the cost of size. `-p:PublishSingleFile=true` bundles the app into one executable; framework-dependent single-file is allowed, while `EnableCompressionInSingleFile` requires self-contained. `-p:PublishReadyToRun=true` adds precompiled native code to the assemblies so startup does less JIT work, making the files larger. If any of these is set without `-r`, the SDK infers the RID of the machine running it.

code

bash · 7 lines
bash
# Linux print server with no .NET installed
dotnet publish src/PressCtl -r linux-x64 --self-contained \
  -p:PublishSingleFile=true -p:PublishReadyToRun=true
# -> src/PressCtl/bin/Release/net10.0/linux-x64/publish/PressCtl

# Windows PCs that already run .NET 10: small framework-dependent exe
dotnet publish src/PressCtl -r win-x64 -p:PublishSingleFile=true

go deeper

for a junior

Know the two deployment modes: framework-dependent needs the runtime installed, self-contained carries it. Know that -r names the target OS and CPU.

for a middle

Explain how -r, --self-contained, PublishSingleFile and PublishReadyToRun combine, which ones need a RID, and the .NET 8 change that stopped a RID implying self-contained.

for a senior

Show the production judgement: always pass -r per target in CI, weigh self-contained size against runtime patching, and know that RID inference uses the build agent's platform.

for a principal

Discuss the fleet trade-off: self-contained isolates each tool from machine-wide runtime upgrades but moves security patching into your release process, which needs a republish cadence.

## The scenario A print shop ships `PressCtl`, a command-line tool that sends jobs to its printers. Office PCs run Windows on x64; the print server is Linux on x64 and has no .NET runtime installed. The publish switches decide what each machine receives. ## Framework-dependent vs self-contained - **Framework-dependent** (the default): the publish folder holds the app's assemblies and its package dependencies, but not the runtime. The target machine must have a compatible .NET runtime (here .NET 10), and patch versions roll forward automatically. - **Self-contained** (`--self-contained` or `--sc`, MSBuild `SelfContained=true`): the runtime and the base libraries are copied into the folder, so the app runs on a machine with no .NET installed. The folder is much larger, and you ship runtime security patches by republishing. ## The runtime identifier A **runtime identifier (RID)** such as `win-x64`, `linux-x64` or `osx-arm64` names an OS and CPU. `dotnet publish -r linux-x64` selects platform-specific package assets, produces a native launcher (apphost) for that platform, and writes to `bin/Release/net10.0/linux-x64/publish/`. A **.NET 8 breaking change** matters here: for `net8.0` and later targets a RID **no longer implies self-contained**. On older targets `-r` silently bundled the runtime; today it does not, and the SDK's targets only fall back to `SelfContained=false`. So the print server needs `-r linux-x64 --self-contained`. ## Single-file `PublishSingleFile=true` bundles the managed assemblies into the apphost, giving one executable to copy: 1. It requires a RID, because the bundle is a native executable. 2. It works framework-dependent too; the file is then small but still needs the runtime installed. 3. Native libraries stay beside the executable unless `IncludeNativeLibrariesForSelfExtract=true`. 4. `EnableCompressionInSingleFile=true` compresses the bundle, and the SDK raises an error unless the publish is also self-contained. ## ReadyToRun `PublishReadyToRun=true` compiles the app's assemblies ahead of time into the **ReadyToRun** format: the IL stays, and precompiled native code is added beside it. Startup does less just-in-time compilation, and the assemblies get noticeably larger. Like single-file it needs a RID, because the native code targets one platform. (How the runtime then uses that code is the CLR topic's subject; PublishAot, which removes the JIT entirely, is the Native AOT topic's.) ## RID inference: the CI trap When `--self-contained`, `PublishSingleFile`, `PublishReadyToRun` or `PublishAot` is set but no RID is given, the SDK sets `UseCurrentRuntimeIdentifier` and uses the RID of **the machine running the SDK**. Publishing `PressCtl` on a Windows build agent with `-p:PublishSingleFile=true` and no `-r` therefore yields a `win-x64` executable, which will not start on the Linux print server. Always pass `-r` explicitly for every target platform. ## Putting it together | Goal | Command | |---|---| | Windows PCs that already have .NET 10 | `dotnet publish -r win-x64 -p:PublishSingleFile=true` | | Linux server with no runtime | `dotnet publish -r linux-x64 --sc -p:PublishSingleFile=true` | | Faster cold start on the server | add `-p:PublishReadyToRun=true` | These properties can also live in the project file so that every publish is consistent; command-line `-p` values override them for one run. ## Checking what you built - The publish folder of a framework-dependent app contains a `.runtimeconfig.json` naming the framework it needs; a self-contained folder instead holds the runtime's own libraries next to the app. - A single-file publish leaves one executable plus, by default, any native libraries and the `.pdb` symbols beside it; symbols are not embedded unless you configure it. - Size is the quickest sanity check: a self-contained folder is tens of megabytes, a framework-dependent one is close to the size of your own assemblies. In an interview, state the matrix out loud: who installs the runtime (you or the target machine), how many files you copy, and how fast the tool must start. The switches follow from those three answers rather than from habit.

  • Why does a framework-dependent single-file publish still need -r?
    The single-file bundle is the native apphost with the managed assemblies appended, and an apphost exists per operating system and CPU. The SDK cannot build one without a runtime identifier, so it either uses the one you pass or infers the current machine's RID.
  • What is the operational cost of self-contained deployments?
    Each app carries its own copy of the runtime, so a .NET security patch only reaches it when you republish and redeploy. Framework-dependent apps pick up an installed runtime patch automatically, because patch versions roll forward.
  • Where do native libraries go in a single-file publish?
    By default they stay as separate files next to the executable. Setting `IncludeNativeLibrariesForSelfExtract=true` puts them in the bundle, and they are extracted to disk at startup.

  • Passing -r linux-x64 makes a net10.0 publish self-contained automatically.
  • A single-file executable always contains the .NET runtime.
  • ReadyToRun assemblies are smaller than the plain IL build output.
  • --self-contained is ignored unless -r is also given.
  • EnableCompressionInSingleFile works for framework-dependent single-file apps.

Assumes the .NET 10 SDK and a net10.0 project; since net8.0 a runtime identifier no longer implies a self-contained publish.