NuGet restore and dependency locking
NuGet restore and dependency locking
NuGet restore resolves package dependencies, downloads or reuses approved package assets, and records the graph for the build. A declared version constraint and the resolved version are different things. A lock file captures the result for a particular project and target framework; locked restore checks that result rather than accepting a new graph silently.
Proposed baseline: .NET SDK 10.0.404, net10.0 / C# 14. All results are reasoned, not executed. Build and test examples require an approved isolated environment. Framework examples are separate test classes, not console entry points.
Read a version constraint
This is a documentary fragment using invented package IDs, not a package to download. A bare version usually denotes a minimum, while brackets around one version request exactly that version.
<ItemGroup> <PackageReference Include="Example.Rates" Version="[1.2.0,2.0.0)" /> <PackageReference Include="Example.Format" Version="[3.1.0]" /></ItemGroup>For a simple graph with Rates versions 1.2.0, 1.4.0, 2.0.0 and no other constraints, lowest-applicable resolution selects 1.2.0. Other graph rules, e.g. direct dependency wins, matter in real restores.
Expected observation (reasoned, not executed):
Rates may resolve to an available compatible version at least 1.2.0 and below 2.0.0; Format requires 3.1.0.Centralize versions
Documentary Directory.Packages.props. A consuming project uses PackageReference Include="Example.Rates" without its own Version. Keep central settings under source control.
<Project> <PropertyGroup> <ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally> </PropertyGroup> <ItemGroup> <PackageVersion Include="Example.Rates" Version="1.2.0" /> </ItemGroup></Project>Central management coordinates constraints; it does not prove each project resolves identical graphs. Review target-specific conditions, overrides and nearest imported central file.
Expected observation (reasoned, not executed):
The centrally declared version supplies the dependency constraint to projects that reference the package under this configuration.Review a locked restore workflow
Run only in the package-free local Rates lab after reviewing its configuration. The first command generates the lock file; review it before the second command. For real package graphs use a preapproved mirror and vulnerability audit sources.
dotnet restore App/App.csproj --use-lock-file --configfile NuGet.Configdotnet restore App/App.csproj --locked-mode --configfile NuGet.Configdotnet build App/App.csproj --no-restorepackages.lock.json records the resolved graph and package hashes where applicable; obj/project.assets.json is build input derived during restore. Never fabricate a lock file or call a restore successful without actually running it. The lab has no third-party packages.
Expected observation (reasoned, not executed):
With required SDK packs and unchanged inputs, locked restore should accept the generated graph; changed constraints may cause failure. Build uses restored assets.Common mistakes and edge cases
A lock file is not a vulnerability assessment, and an audit report is not proof of safe code. Map direct and transitive IDs to approved package sources and review cached packages too; a mapping does not retroactively authenticate existing cache contents. NuGet can import executable build logic and analyzers. Do not suppress restore warnings or change feeds simply to make a demonstration pass. In .NET 10, dotnet package list may restore automatically; use --no-restore when inspecting already restored assets and supported by the command.
Practice and recap
A package permits [1.2.0,2.0.0). The approved mirror offers 1.1.0, 1.3.0 and 2.0.0. Choose a version under the simple lowest-applicable rule; then explain what locked restore should do if the constraint changes.
Hint: The lower bound is inclusive and the upper bound exclusive.
Learning objective: Distinguish constraints from resolved package versions. See accompanying manual practice and knowledge check.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
NuGet restore and dependency locking — knowledge check
Apply the lesson concepts to the following scenarios.
What does [1.2.0,2.0.0) exclude?
What does locked restore check?
Where are central package versions commonly declared?
What is a transitive dependency?
Why configure source mapping?