Skip to content

3.8 — The .NET CLI

← Roadmap · Section: 3. .NET Runtime and Platform Prev: 3.7 csproj and target frameworks · Next: 3.9 .NET Standard

Markers: ⭐ 💻 · Confidence: 🔴 · Last reviewed: —


TL;DR

The .NET CLI is the dotnet command — a single tool that creates, builds, tests, runs and publishes .NET projects from a terminal, with no IDE involved.

  • It is what your CI pipeline actually runs. Visual Studio calls the same underlying build engine.
  • The commands you need daily: new, restore, build, run, test, publish.
  • build and publish are different. build compiles for development; publish produces the deployable output.

Definitions

Term What it is What it means
CLI a command-line tool Command-Line Interface. The dotnet command.
dotnet new a command Creates a project or file from a template.
dotnet restore a command Downloads the NuGet packages a project needs. Usually implicit.
dotnet build a command Compiles the project. Output goes to bin/. For development.
dotnet run a command Builds and then runs the project. For development only.
dotnet publish a command Produces the deployable output — trimmed, optimised, with deps.json.
dotnet test a command Builds and runs the test projects.
dotnet tool a command group Installs extra CLI tools, globally or per project.
Configuration a build setting Debug (default, no optimisation, full symbols) or Release (optimised).
Framework-dependent a publish mode Output needs .NET installed on the target machine. Small.
Self-contained a publish mode Output includes the runtime. Needs nothing installed. Large.
Global tool an installed CLI tool Available anywhere on the machine, like dotnet-ef or dotnet-counters.
Local tool a project-scoped tool Pinned per repository in .config/dotnet-tools.json, so everyone gets the same version.

The concept

Why learn the CLI when you have an IDE?

Three reasons, and the first is the one that matters in interviews.

1. CI/CD runs the CLI, not your IDE. Every GitHub Actions or Azure DevOps pipeline for .NET is a sequence of dotnet commands. If you cannot build from a terminal, you cannot debug a broken pipeline.

2. It is the same build. Visual Studio, Rider and dotnet build all drive MSBuild with the same project file. "It works in Visual Studio but not in CI" almost always means an environment difference — a missing package source, a different SDK version — not a genuinely different build.

3. It works everywhere. Linux containers have no IDE.


How it works

The simple version — the daily commands

# Create things
dotnet new webapi -o MyApp.Api          # a Web API project
dotnet new classlib -o MyApp.Core       # a class library
dotnet new xunit -o MyApp.Tests         # a test project
dotnet new sln -n MyApp                 # a solution file
dotnet new gitignore                    # a .NET-aware .gitignore

# Wire them together
dotnet sln add MyApp.Api/MyApp.Api.csproj
dotnet add MyApp.Api reference MyApp.Core
dotnet add MyApp.Api package Serilog.AspNetCore

# Work
dotnet build                            # compile
dotnet run --project MyApp.Api          # build and run
dotnet watch --project MyApp.Api        # rerun automatically on file change
dotnet test                             # run all tests

# Ship
dotnet publish -c Release -o ./out

dotnet new list shows every available template, and templates can be installed from NuGet — Clean Architecture and other solution starters are distributed this way.

dotnet watch is the one people underuse. It rebuilds and restarts on every save, and for a web app it also refreshes the browser. It removes the stop-edit-start loop entirely.

⚠️ build versus publish

This distinction gets asked about, and it matters in practice.

dotnet build dotnet publish
Purpose Development Deployment
Output bin/Debug/net10.0/ bin/Release/net10.0/publish/
Includes dependencies? Only project ones All of them
Includes deps.json, runtimeconfig.json? Partly Yes, complete
Ready to copy to a server? No Yes

Never deploy the output of dotnet build. It is missing pieces the runtime needs to resolve assemblies (3.6), and it defaults to a Debug build with no optimisation. This is a genuinely common mistake — the app may even start, then fail on a code path that needs a package that was not copied.

Framework-dependent versus self-contained

# Framework-dependent (default) — small, needs .NET installed on the target
dotnet publish -c Release
# → about 1 MB

# Self-contained — carries the runtime, needs nothing installed
dotnet publish -c Release -r linux-x64 --self-contained
# → about 70 MB

# Self-contained + trimmed — unused framework code removed
dotnet publish -c Release -r linux-x64 --self-contained -p:PublishTrimmed=true
# → about 30 MB

# Single file — everything in one executable
dotnet publish -c Release -r linux-x64 --self-contained \
    -p:PublishSingleFile=true -p:PublishTrimmed=true

In Docker you normally want framework-dependent, because the base image already contains the runtime. Self-contained is for machines you do not control. See 24.1.

⚠️ PublishTrimmed removes code it believes is unused. Anything reached only through reflection looks unused and gets deleted, producing a runtime failure that never happened in development. Same underlying problem as Native AOT — see 3.2.

How it actually works — tools

Global tools install once and work anywhere:

dotnet tool install -g dotnet-ef              # EF Core migrations
dotnet tool install -g dotnet-counters        # live performance metrics
dotnet tool install -g dotnet-dump            # memory dumps
dotnet tool install -g dotnet-trace           # performance traces
dotnet tool install -g dotnet-outdated-tool   # find outdated packages

Those middle three are the production diagnostics set from 3.5 and 18.9. Worth being able to name them.

Local tools are pinned per repository, so every developer and the CI agent use the same version:

dotnet new tool-manifest                      # creates .config/dotnet-tools.json
dotnet tool install dotnet-ef                 # no -g: local to this repo
dotnet tool restore                           # what CI runs
dotnet ef migrations add Initial              # runs the pinned version

Prefer local tools for anything a build depends on. A global dotnet-ef that is three versions ahead of the project's EF Core packages produces confusing failures.

The deeper detail — commands worth knowing

# Diagnosing dependency problems
dotnet list package --include-transitive      # everything you actually depend on
dotnet list package --outdated                # newer versions available
dotnet list package --vulnerable              # known CVEs — put this in CI
dotnet nuget locals all --clear               # clear the package cache

# Build troubleshooting
dotnet build -v detailed                      # verbose MSBuild output
dotnet build --no-restore                     # skip restore when it is already done
dotnet clean                                  # delete bin and obj

# Testing
dotnet test --filter "Category=Integration"
dotnet test --collect:"XPlat Code Coverage"   # coverage via coverlet
dotnet test --logger "trx;LogFileName=results.trx"

# Info
dotnet --info                                 # SDKs, runtimes, OS, everything
dotnet --list-sdks
dotnet --list-runtimes

dotnet --info is the first thing to run when a build behaves differently on two machines. It shows the SDK version, the runtimes present, and the OS — which is usually where the difference is.

Controlling the SDK version

A global.json pins which SDK a repository uses:

{
  "sdk": {
    "version": "10.0.100",
    "rollForward": "latestFeature"
  }
}

Without this, whoever has the newest SDK installed builds with it, and subtle differences appear between machines. Worth adding to any repository with more than one developer.


Code

A realistic CI pipeline is just these commands in order:

# .github/workflows/ci.yml
steps:
  - uses: actions/checkout@v4

  - uses: actions/setup-dotnet@v4
    with:
      dotnet-version: '10.0.x'

  - name: Restore
    run: dotnet restore

  - name: Build
    run: dotnet build --no-restore -c Release

  - name: Test
    run: dotnet test --no-build -c Release --logger trx

  - name: Check for vulnerable packages
    run: dotnet list package --vulnerable --include-transitive

  - name: Publish
    run: dotnet publish -c Release -o ./out --no-build

Two details worth noticing:

  • --no-restore and --no-build stop each step redoing the previous one's work. On a large solution that saves minutes per run.
  • The steps mirror exactly what you would type locally, which is why being fluent in the CLI makes pipeline problems easy to reproduce on your own machine.

Interview questions

Q: What is the difference between dotnet build and dotnet publish?

build compiles the project for development — output goes to bin/Debug, and it does not gather everything needed to run elsewhere. publish produces the deployable output: all dependencies copied in, plus the deps.json and runtimeconfig.json the runtime needs to resolve assemblies at startup. Deploying the output of build is a common mistake — it may start and then fail on a code path needing a package that was never copied, and it defaults to an unoptimised Debug build.

Q: What is the difference between framework-dependent and self-contained publishing?

Framework-dependent output relies on .NET being installed on the target machine, so it is small — around a megabyte. Self-contained output includes the entire runtime, so it needs nothing installed but is much larger, around seventy megabytes before trimming. In Docker you normally want framework-dependent because the base image already has the runtime. Self-contained suits machines you do not control, or where you cannot guarantee a runtime version.

Q: What are local tools and why use them over global ones?

A global tool is installed once per machine and available anywhere. A local tool is pinned per repository in a manifest file, so dotnet tool restore gives every developer and the CI agent the exact same version. That matters for anything the build depends on — dotnet-ef is the usual example, because a globally installed version that is ahead of the project's EF Core packages produces confusing failures that only affect one machine.

Q: A colleague says the build works locally but fails in CI. Where do you start?

dotnet --info on both, because it shows the SDK version, installed runtimes and OS, and the difference is usually there. If the SDK versions differ, a global.json pinning the version fixes it permanently. After that I would check the package sources, since a private feed configured locally but not in CI produces restore failures, and confirm the CI is running the same commands rather than a different build path. The underlying point is that Visual Studio and dotnet build drive the same engine with the same project file, so a genuine difference in output almost always means an environment difference.


Traps ⚠️

  • Deploying dotnet build output instead of dotnet publish. Missing dependencies and runtime config files.
  • Forgetting -c Release. Debug builds are unoptimised and much slower.
  • PublishTrimmed silently removes reflection-only code. Fails at runtime, not at build.
  • A global dotnet-ef mismatched with the project's EF Core version causes confusing errors. Use a local tool.
  • No global.json means whoever has the newest SDK builds differently.
  • dotnet run is not for production. It builds first and adds overhead — run the published DLL directly.
  • --no-build without a preceding build fails, unhelpfully.
  • linux-x64 is not Alpine. Alpine needs linux-musl-x64.


Sources