← All posts

Clean Restore and Verify a .NET Microservice

This guide shows how to wipe every local NuGet cache and build output, then restore, build and test a service from scratch. Use it to confirm that a service builds on a fresh machine, for example after changing `nuget.config` or package versions. The examples use the **casa** service. For another service, swap in its folder and solution file.

Prerequisites

  • .NET SDK installed (dotnet --version)
  • Network access to nuget.org and the internal Nexus NuGet feed
  • A normal PowerShell window. Administrator rights are not needed, so don't use "Run as administrator".

Before you start

Always use full paths.
PowerShell often opens in C:\Windows\system32. A delete command with a relative path (such as -Path .) will run against whatever folder you're in. Every command in this guide uses an absolute path, so it is safe to run from anywhere.

Close your IDE.
VS Code (C# Dev Kit), Visual Studio and Rider restore packages automatically. If one is open, it recreates bin/obj folders and refills the cache seconds after you delete them, and your clean test is no longer clean.


Variables

Set these once per PowerShell session. The later steps use them.

$ServiceRoot = "D:\Sober\casa\src"
$Solution    = "D:\Sober\casa\src\OneApp.Casa.slnx"
$WebApi      = "D:\Sober\casa\src\OneApp.Casa.WebApi"

Step 1: Stop build servers

Stops background MSBuild and compiler processes that may hold files open.

dotnet build-server shutdown

Step 2: Clear all NuGet caches

Clears the global packages folder, the HTTP cache, the temp folder and the plugins cache.

dotnet nuget locals all --clear

Check that the cache is empty:

Test-Path "$env:USERPROFILE\.nuget\packages\oneapp.framework"

Expected: False


Step 3: Delete build output

Preview first:

Get-ChildItem -Path $ServiceRoot -Include bin,obj -Directory -Recurse | Select-Object FullName

Every path should be inside the service folder. Then delete:

Get-ChildItem -Path $ServiceRoot -Include bin,obj -Directory -Recurse | Remove-Item -Recurse -Force

Check:

(Get-ChildItem -Path $ServiceRoot -Include bin,obj -Directory -Recurse).Count

Expected: 0. If the count is above zero, an IDE is still open and recreating the folders. See Before you start.


Step 4: Check package sources

Shows which feeds NuGet will use for this service.

dotnet nuget list source --configfile "$ServiceRoot\nuget.config"

Expected: only nuget.org and nexus, both Enabled.


Step 5: Restore

Downloads every package fresh, ignoring any cache.

dotnet restore $Solution --no-cache --force

Expected: Restore succeeded

Result Meaning Action
Restore succeeded All packages were downloaded. Continue.
401 Unauthorized / 403 Forbidden The feed requires a login. Supply credentials at build time (see Feed credentials). Never commit them.
503 Service Unavailable / End of Central Directory record could not be found Nexus server problem. Report it to the Nexus administrator and retry later.
NU1101 Unable to find package The package or version isn't on the feed. Check the version in Directory.Packages.props.
NU1902 / NU1903 warning A package has a known vulnerability. Doesn't block the build. Track it and upgrade the package.

Step 6: Build

dotnet build $Solution --no-restore

Expected: Build succeeded. Warnings don't block the build; errors do.


Step 7: Run tests

dotnet test $Solution --no-build

Expected: Failed: 0

Integration tests may need Kafka, Redis or a database. If only integration tests fail with connection errors, check that those dependencies are running before you treat it as a code problem.


Step 8: Run the service (optional)

dotnet run --project $WebApi --no-build

Expected: the app starts and prints Now listening on: http://.... Press Ctrl+C to stop it.


Step 9: Docker build (optional)

This is the closest match to CI, because the container starts with no cache at all. Run it from the folder that contains the Dockerfile:

Set-Location $ServiceRoot
docker build --no-cache -t casa-check .

Expected: the image builds with no restore errors.


Quick reference

All steps in one block, for when you've done this before:

$ServiceRoot = "D:\Sober\casa\src"
$Solution    = "D:\Sober\casa\src\OneApp.Casa.slnx"

dotnet build-server shutdown
dotnet nuget locals all --clear
Get-ChildItem -Path $ServiceRoot -Include bin,obj -Directory -Recurse | Remove-Item -Recurse -Force
dotnet restore $Solution --no-cache --force
dotnet build $Solution --no-restore
dotnet test $Solution --no-build

Feed credentials

nuget.config must not contain usernames or passwords. Security scanners (Coverity / Black Duck, CWE-798) flag them, and anyone with access to the repository or the Docker image can read them.

The Nexus feed allows anonymous reads, so no credentials are needed today. If that changes, add them at build time from a secret store or CI variables:

dotnet nuget update source nexus `
  --username $env:NEXUS_USER `
  --password $env:NEXUS_PASS `
  --store-password-in-clear-text `
  --configfile "$ServiceRoot\nuget.config"

Don't commit the file after running this command.

NuGet matches credentials to a source by name. A <packageSourceCredentials> entry is used only if its element name exactly matches a key in <packageSources>. If a source is renamed, its old credentials entry stops working without any error.


Troubleshooting

Problem Cause Fix
bin/obj count isn't 0 after deleting IDE auto-restore Close the IDE and repeat Step 3.
Access to the path ... is denied on C:\Windows\... Command ran with a relative path from system32 Always use the absolute paths from this guide.
being used by another process IDE or build server holds the files Run Step 1, close the IDE, try again.
DISM / sfc say elevation is required These need admin rights Not needed for this guide. Contact IT only if Windows itself misbehaves.
5
Min read
904
Words
2
Topics
105
Views
8 days ago
Published
8 days ago
Updated

No comments yet.

Sign in to leave a comment.