TExcelTAD
Home › .NET Builds: MSBuild & NuGet › dotnet publish: Deployment Modes

dotnet publish: Deployment Modes

dotnet build produces output for you: it's fine to run from bin/ on a machine with the SDK. dotnet publish produces output for a target machine: the app, its dependencies and the files needed to start it, in one folder you can copy, zip or put in a container image. The interesting decisions are how much of .NET goes into that folder and in what shape.

Framework-dependent (the default)

bash · try it yourself
mkdir app && cd app
cat > app.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
  </ItemGroup>
</Project>
XML
echo 'System.Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(new { ok = true }));' > Program.cs
dotnet publish -o out | grep -v X.509
ls out
cat out/app.runtimeconfig.json; echo
dotnet out/app.dll
  Determining projects to restore...
  Restored /home/learner/app/app.csproj (in 392 ms).
  app -> /home/learner/app/bin/Release/net10.0/app.dll
  app -> /home/learner/app/out/
Newtonsoft.Json.dll
app
app.deps.json
app.dll
app.pdb
app.runtimeconfig.json
{
  "runtimeOptions": {
    "tfm": "net10.0",
    "framework": {
      "name": "Microsoft.NETCore.App",
      "version": "10.0.0"
    },
    "configProperties": {
      "System.Reflection.Metadata.MetadataUpdater.IsSupported": false,
      "System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization": false
    }
  }
}
{"ok":true}
  • Since .NET 8, dotnet publish uses the Release configuration by default (see bin/Release/ in the log).
  • out/ holds the app (app.dll), its package dependencies (Newtonsoft.Json.dll), app.deps.json (the dependency list the host uses to resolve assemblies) and app.runtimeconfig.json, which names the shared framework the app needs: Microsoft.NETCore.App 10.0, rolled forward to newer patch versions automatically.
  • The target machine must have a matching .NET runtime installed. In exchange the folder is tiny, and security patches to the runtime reach the app without republishing.

The app file next to the DLL is the apphost, a small native launcher for the build machine's platform. Pass a runtime identifier (RID) to build for a specific platform:

bash · try it yourself
mkdir app && cd app
cat > app.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
  </PropertyGroup>
</Project>
XML
echo 'System.Console.WriteLine(
quot;running on {System.Runtime.InteropServices.RuntimeInformation.RuntimeIdentifier}");' > Program.cs dotnet publish -r linux-x64 --self-contained false -o out -nologo | grep -- '->' file out/app | cut -d, -f1 ./out/app
  app -> /home/learner/app/bin/Release/net10.0/linux-x64/app.dll
  app -> /home/learner/app/out/
out/app: ELF 64-bit LSB pie executable
running on linux-x64

A RID-specific, framework-dependent publish still needs the runtime installed, but its apphost is native for the RID you chose (linux-x64, linux-arm64, win-x64, osx-arm64…).

Self-contained, single-file, trimmed, native AOT

The other modes put the runtime itself into the output:

Mode Property / flag Output Needs at publish time
Self-contained --self-contained with -r <rid> app + the whole runtime (~70 MB); runs without .NET installed the runtime pack Microsoft.NETCore.App.Runtime.<rid>
Single-file -p:PublishSingleFile=true one executable (the runtime can be inside it too) runtime pack, plus the ILLink analyzers
Trimmed -p:PublishTrimmed=true (self-contained only) unused code removed from the app and the runtime runtime pack + Microsoft.NET.ILLink.Tasks
Native AOT -p:PublishAot=true one native executable, no JIT, fast start-up runtime pack + the ILCompiler package + a C toolchain

Runtime packs are NuGet packages, downloaded on first use like the targeting packs in the multi-targeting lesson. The offline feed of this course has none of them, so here is what each mode asks for:

bash · try it yourself
mkdir app && cd app
cat > app.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
  </PropertyGroup>
</Project>
XML
echo 'System.Console.WriteLine("hi");' > Program.cs
try() { echo "== $*"; dotnet publish -r linux-x64 "$@" -o out 2>&1 | grep -o 'Unable to find package [^ .]*\(\.[A-Za-z0-9-]*\)*' | sort -u; }
try --self-contained
try -p:PublishSingleFile=true
try -p:PublishTrimmed=true
try -p:PublishAot=true
== --self-contained
Unable to find package Microsoft.AspNetCore.App.Runtime.linux-x64.
Unable to find package Microsoft.NETCore.App.Runtime.linux-x64.
== -p:PublishSingleFile=true
Unable to find package Microsoft.AspNetCore.App.Runtime.linux-x64.
Unable to find package Microsoft.NET.ILLink.Tasks.
Unable to find package Microsoft.NETCore.App.Runtime.linux-x64.
== -p:PublishTrimmed=true
Unable to find package Microsoft.AspNetCore.App.Runtime.linux-x64.
Unable to find package Microsoft.NET.ILLink.Tasks.
Unable to find package Microsoft.NETCore.App.Runtime.linux-x64.
== -p:PublishAot=true
Unable to find package Microsoft.AspNetCore.App.Runtime.linux-x64.
Unable to find package Microsoft.DotNet.ILCompiler.
Unable to find package Microsoft.NET.ILLink.Tasks.
Unable to find package Microsoft.NETCore.App.Runtime.NativeAOT.linux-x64.
Unable to find package Microsoft.NETCore.App.Runtime.linux-x64.
Unable to find package runtime.linux-x64.Microsoft.DotNet.ILCompiler.

The ASP.NET Core runtime pack shows up even for this console app: a RID-specific restore requests runtime packs for the shared frameworks the SDK knows about, not only for the one the app uses. That list is the real shopping list for an air-gapped build agent: mirror those packages to the internal feed for every RID you ship, and these modes work offline too. On a connected machine a self-contained publish just works:

bash
$ dotnet publish -r linux-x64 --self-contained -p:PublishSingleFile=true -o out
$ ls out
app  app.pdb
$ ./out/app        # no .NET installation needed on the target

(Sample session, not run here.)

Trimming and AOT: read the warnings

Trimming removes code that static analysis can't see being used, and AOT compiles ahead of time without a JIT. Both break code that relies on reflection over types decided at run time: classic Newtonsoft.Json serialization, plug-in loading, some dependency-injection and ORM patterns. The SDK reports these spots as warnings (IL2026, IL3050 and friends) during publish and, with <IsTrimmable>true</IsTrimmable> or <IsAotCompatible>true</IsAotCompatible> on a library, during normal builds. Treat them as errors: a trimmed app that compiled with trim warnings can fail at run time with a missing method. Prefer source-generated alternatives (System.Text.Json source generation, [LoggerMessage], compile-time DI).

Choosing a mode

Situation Usual choice
Container image based on mcr.microsoft.com/dotnet/aspnet or runtime framework-dependent (the runtime is in the base image)
Server you manage, runtime kept patched framework-dependent
Customers' machines, no guaranteed runtime self-contained, often single-file
CLI tools, serverless functions, tiny containers (runtime-deps) native AOT, if the code and libraries are AOT-compatible

Test yourself

Which configuration does dotnet publish use by default with the .NET 8+ SDK?

  1. Debug
  2. Release
  3. Whatever the last build used
  4. Publish

What does a framework-dependent app's runtimeconfig.json tell the host?

  1. Which NuGet feed to use
  2. Which shared framework (name and version) to load
  3. The app's version number
  4. Where the source code is

Why did --self-contained fail offline in this course's sandbox?

  1. Self-contained apps can't run on Linux
  2. It needs the runtime pack Microsoft.NETCore.App.Runtime.linux-x64, a NuGet package not in the feed
  3. It needs Visual Studio
  4. The apphost is missing