繁体   English   中英

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

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

在过去的 .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

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

在 docs.microsoft.com 上查看NuGet API v3以获取有关 NuGet 3 的信息。在撰写本文时,该文档基本上是一个大待办事项,没有太多信息。

这里有一些更有用的博客文章。

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

探索 NuGet v3 库,第 2 部分

探索 NuGet v3 库,第 3 部分

当然,您可以随时通过 NuGet 的源代码进行探索以找到更多示例。 大多数核心逻辑位于https://github.com/nuget/nuget.client 中

实现它的最佳方法是在您的项目中引用NugetDownloader Nuget 包,并使用它以编程方式下载任何其他包

Install-Package NugetDownloader

NuGet 徽章

源代码和帮助指南可在以下网址获得: 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因为它有一些类似的实现,但是代码理解和提取太多了。

我也一直在寻求这样做,并找到了有关如何做到这一点的 Microsoft 指南。 一旦您知道流程的切入点,其他一切都应该是直截了当的。

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

指南: https : //docs.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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM