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

16

以前在使用.NET Framework时,我用这个示例来编写与nuget的程序交互:

与包玩耍,以编程方式!

那么,在.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"));

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

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

2
你尝试过使用V3链接作为存储库路径吗? - Mathivanan KP
@MathivananKP,我尝试过了,但是"NuGet.Core"程序集在.NET Core上无法工作。 - NBM
3个回答

10
您展示的代码示例使用了不支持.NET Core的NuGet 2。您需要使用NuGet 3或即将发布的NuGet 4,这些API与NuGet 2有很大的区别。其中一个重大变化是NuGet.Core已经过时,不会被移植到.NET Core上。
在learn.microsoft.com上查看NuGet API v3获取有关NuGet 3的信息。撰写本文时,该文档基本上是一个巨大的TODO,没有太多信息。
以下是一些更有用的博客文章:

探索NuGet v3库,第1部分介绍和概念

探索NuGet v3库,第2部分

探索 NuGet v3 库,第三部分

当然,您也可以通过浏览 NuGet 的源代码来查找更多示例。大多数核心逻辑位于 https://github.com/nuget/nuget.client


2

我也一直在寻找如何做到这一点,并找到了微软的指南,可以准确地说明如何做到这一点。一旦你知道进入过程的入口点,其他所有事情都应该很简单。

这适用于.NET Core(在3.1上测试)并使用Nuget v3。

指南:https://learn.microsoft.com/en-us/nuget/reference/nuget-client-sdk

列出软件包的示例:

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

下载软件包的示例:

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

1

实现这个最好的方法是在你的项目中引用NugetDownloader Nuget包,并使用它来以编程方式下载任何其他包。

Install-Package NugetDownloader

NuGet Badge

源代码和帮助指南都可以在以下链接中找到: https://github.com/paraspatidar/NugetDownloader

这里是一个快速示例,演示如何实现:

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();

示例客户端也可在https://github.com/paraspatidar/NugetDownloader/tree/master/NugetDownloaderTestConsole找到。

或者,如果您想从头开始构建Nugetdownloader引擎,则可以参考https://github.com/Azure/azure-functions-host/blob/dev/src/WebJobs.Script/Description/DotNet/PackageManager.cs,因为它具有类似的实现,但需要更多的代码理解和提取。


在回答中提到你是建议工具的作者之一是一种惯例。 - mark

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接