如何在.NET Core中以编程方式从nuget下载nupkg包?

问题描述:

在过去的.NET框架我用这个例子用的NuGet工作程序如何在.NET Core中以编程方式从nuget下载nupkg包?

Play with Packages, programmatically!

是否有.NET睿任何等效源?

//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")); 

我想以编程方式下载和安装任何软件包。

https://api.nuget.org/v3/index.json 
+2

你有没有尝试过使用V3链接作为存储库路径? –

+0

@MathivananKP,我试过但“NuGet.Core”程序集无法在.NET Core上运行。 – NBM

您显示的代码示例使用了NuGet 2,它在.NET Core上不受支持。您需要使用NuGet 3或(即将发布的)NuGet 4.这些API与NuGet 2是一个巨大的突破。其中一个重大变化是NuGet.Core已过时,不会移植到.NET Core。

在docs.microsoft.com上为NuGet 3上的信息签出NuGet API v3。在编写本文时,这个文档基本上是一个大的TODO,并没有太多的信息。

以下是一些更有用的博客文章。

Exploring the NuGet v3 Libraries, Part 1 Introduction and concepts

Exploring the NuGet v3 Libraries, Part 2

Exploring the NuGet v3 Libraries, Part 3

和当然,你可以随时办理的NuGet的源代码洞穴探险找到更多的例子。大部分核心逻辑都在https://github.com/nuget/nuget.client之间。