microsoft/aspire · error · NuGetPackageCacheException

Failed to search for packages. Exit code

Error message

Failed to search for packages. Exit code: {0}.

What it means

NuGetPackageCache.GetPackagesAsync shells out to a NuGet search process (e.g. `dotnet package search`) and throws NuGetPackageCacheException when that process returns a non-zero exit code. The wrapped exit code identifies the underlying NuGet/dotnet CLI failure. This is how the Aspire CLI signals that package search could not complete.

Solutions

  1. Run `dotnet nuget list source` and verify the feed is reachable; fix or remove broken sources.
  2. Re-run the same search manually (dotnet package search) to see the real error and exit code.
  3. Check network/proxy/VPN connectivity to nuget.org.
  4. Update the .NET SDK so the search command is supported.

Example fix

// before: relying on ambient NuGet config with broken feeds
var packages = await cache.GetPackagesAsync("aspire", false, false, ct);
// after: ensure a healthy source before searching
var psi = Process.Start(new ProcessStartInfo("dotnet", "nuget list source"));
psi.WaitForExit();
if (psi.ExitCode != 0) { /* repair NuGet.config first */ }
var packages = await cache.GetPackagesAsync("aspire", false, false, ct);
Defensive patterns

Strategy: try-catch

Validate before calling

var psi = System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo("dotnet", "nuget list source"));
psi.WaitForExit();
bool feedHealthy = psi.ExitCode == 0;

Try / catch

try
{
    var packages = await cache.GetPackagesAsync(term, prerelease, exact, ct);
}
catch (NuGetPackageCacheException ex)
{
    logger.LogError(ex, "Package search failed; check NuGet sources and network.");
    // surface exit code to user
}

Prevention

When it happens

Trigger: Calling GetPackagesAsync (directly or via the `packages`/`aspire add` search flow) when the spawned search process exits non-zero: bad feed, network failure, invalid NuGet.config, or dotnet CLI error.

Common situations: Offline or proxied networks blocking the search endpoint; a broken corporate NuGet.config feed; a `dotnet package search` command not supported by the installed SDK version.

Related errors


AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16). Data as JSON: /api/errors/b34572bc503e7421. Report an issue: GitHub.

Appendix: source

Thrown at src/Aspire.Cli/NuGet/NuGetPackageCache.cs:144

        do
        {
            // This search should pick up Aspire.Hosting.* and CommunityToolkit.Aspire.Hosting.*
            var result = await cliRunner.SearchPackagesAsync(
                workingDirectory,
                query,
                exactMatch: false,
                prerelease,
                SearchPageSize,
                skip,
                nugetConfigFile,
                useCache, // Pass through the useCache parameter
                new ProcessInvocationOptions { SuppressLogging = true },
                cancellationToken
                );

            if (result.ExitCode != 0)
            {
                throw new NuGetPackageCacheException(string.Format(CultureInfo.CurrentCulture, ErrorStrings.FailedToSearchForPackages, result.ExitCode));
            }
            else
            {
                if (result.Packages?.Length > 0)
                {
                    collectedPackages.AddRange(result.Packages);
                }

                if (result.Packages?.Length < SearchPageSize)
                {
                    continueFetching = false;
                }
                else
                {
                    continueFetching = true;
                    skip += SearchPageSize;
                }
            }

View on GitHub (pinned to 25830f84bd)