Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download a nupkg package from nuget programmatically in .NET Core?

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
like image 935
HamedFathi Avatar asked Feb 11 '17 10:02

HamedFathi


People also ask

How do I download NuGet packages in Visual Studio?

Install packages from NuGet.orgNavigate to NuGet.org and search for the package you want to install. Select Package Manager, and then copy the Install-Package command. In Visual Studio, select Tools > NuGet Package Manager > Package Manager Console to open the package manager console.


1 Answers

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 docs.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.

like image 134
natemcmaster Avatar answered Sep 17 '22 13:09

natemcmaster