16

In past with .NET Framework I used this example for working with nuget programmatically

Play with Packages, programmatically!

Is there any equivalent source for .NET Core?

//ID of the package to be looked up
string packageID = "EntityFramework";

//Connect to the official package repository
IPackageRepository repo = PackageRepositoryFactory.Default.CreateRepository("https://packages.nuget.org/api/v2");

//Get the list of all NuGet packages with ID 'EntityFramework'       
List<IPackage> packages = repo.FindPackagesById(packageID).ToList();

//Filter the list of packages that are not Release (Stable) versions
packages = packages.Where (item => (item.IsReleaseVersion() == false)).ToList();

//Iterate through the list and print the full name of the pre-release packages to console
foreach (IPackage p in packages)
{
    Console.WriteLine(p.GetFullName());
}

//---------------------------------------------------------------------------

//ID of the package to be looked up
string packageID = "EntityFramework";

//Connect to the official package repository
IPackageRepository repo = PackageRepositoryFactory.Default.CreateRepository("https://packages.nuget.org/api/v2");

//Initialize the package manager
string path = <PATH_TO_WHERE_THE_PACKAGES_SHOULD_BE_INSTALLED>
PackageManager packageManager = new PackageManager(repo, path);

//Download and unzip the package
packageManager.InstallPackage(packageID, SemanticVersion.Parse("5.0.0"));

I want to download and install any package programmatically.

https://api.nuget.org/v3/index.json
Cœur
  • 37,241
  • 25
  • 195
  • 267
HamedFathi
  • 3,667
  • 5
  • 32
  • 72

3 Answers3

10

The code sample you have shown uses NuGet 2 which is not supported on .NET Core. You'll need to use NuGet 3 or the (soon to be released) NuGet 4. These APIs are a huge break from NuGet 2. One of these breaking changes is that NuGet.Core is obsolete on won't be ported to .NET Core.

Checkout NuGet API v3 on learn.microsoft.com for info on NuGet 3. At the time of writing, this doc is basically a big TODO and doesn't have much info.

Here are some blog posts that are more useful.

Exploring the NuGet v3 Libraries, Part 1 Introduction and concepts

Exploring the NuGet v3 Libraries, Part 2

Exploring the NuGet v3 Libraries, Part 3

And of course, you can always go spelunking through NuGet's source code to find more examples. Most of the core logic lives in https://github.com/nuget/nuget.client.

natemcmaster
  • 25,673
  • 6
  • 78
  • 100
2

I've been looking to do this for a while too and found a Microsoft guide on how to do exactly that. Once you know the entry point into the process, everything else should be straight forward.

This works with .NET Core (tested on 3.1) and uses Nuget v3.

The guide: https://learn.microsoft.com/en-us/nuget/reference/nuget-client-sdk

Example to list packages:

ILogger logger = NullLogger.Instance;
CancellationToken cancellationToken = CancellationToken.None;

SourceCacheContext cache = new SourceCacheContext();
SourceRepository repository = Repository.Factory.GetCoreV3("https://api.nuget.org/v3/index.json");
FindPackageByIdResource resource = await repository.GetResourceAsync<FindPackageByIdResource>();

IEnumerable<NuGetVersion> versions = await resource.GetAllVersionsAsync(
    "Newtonsoft.Json",
    cache,
    logger,
    cancellationToken);

foreach (NuGetVersion version in versions)
{
    Console.WriteLine($"Found version {version}");
}

Example to download a package:

ILogger logger = NullLogger.Instance;
CancellationToken cancellationToken = CancellationToken.None;

SourceCacheContext cache = new SourceCacheContext();
SourceRepository repository = Repository.Factory.GetCoreV3("https://api.nuget.org/v3/index.json");
FindPackageByIdResource resource = await repository.GetResourceAsync<FindPackageByIdResource>();

string packageId = "Newtonsoft.Json";
NuGetVersion packageVersion = new NuGetVersion("12.0.1");
using MemoryStream packageStream = new MemoryStream();

await resource.CopyNupkgToStreamAsync(
    packageId,
    packageVersion,
    packageStream,
    cache,
    logger,
    cancellationToken);

Console.WriteLine($"Downloaded package {packageId} {packageVersion}");

using PackageArchiveReader packageReader = new PackageArchiveReader(packageStream);
NuspecReader nuspecReader = await packageReader.GetNuspecReaderAsync(cancellationToken);
Dharman
  • 30,962
  • 25
  • 85
  • 135
Denis Stepanenko
  • 510
  • 6
  • 14
1

Best way to achieve it is by referring NugetDownloader Nuget package in your Project and use it to download any other package programmatically

Install-Package NugetDownloader

NuGet Badge

Source code and help guide on the same is available at : https://github.com/paraspatidar/NugetDownloader

Here is a quick sample on how to achieve it :

string packageName="Newtonsoft.json";
string version="10.2.1.0"; \\optional

\\initilize NugetEngine from NugetDownloader namespaces

NugetEngine nugetEngine = new NugetEngine();
nugetEngine.GetPackage(packageName, version).Wait();

sample client is also available at https://github.com/paraspatidar/NugetDownloader/tree/master/NugetDownloaderTestConsole

Alternately In case if you want to build your Nugetdownloader engine from scratch , then you might also refer https://github.com/Azure/azure-functions-host/blob/dev/src/WebJobs.Script/Description/DotNet/PackageManager.cs as it has something similar implementation , but that too much of code understanding and extraction.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Paras Patidar
  • 1,024
  • 11
  • 11
  • It is a custom to mention in the answer that you are one of the authors of the suggested utility. – mark Nov 14 '20 at 14:14