简体   繁体   English

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

[英]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在过去的 .NET Framework 中,我使用此示例以编程方式使用 nuget

Play with Packages, programmatically!以编程方式玩包!

Is there any equivalent source for .NET Core? .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

The code sample you have shown uses NuGet 2 which is not supported on .NET Core.您展示的代码示例使用了 .NET Core 不支持的 NuGet 2。 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.您将需要使用 NuGet 3 或(即将发布)NuGet 4。这些 API 是 NuGet 2 的巨大突破。其中一项重大变化是NuGet.Core已过时,不会移植到 .NET核。

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

Here are some blog posts that are more useful.这里有一些更有用的博客文章。

Exploring the NuGet v3 Libraries, Part 1 Introduction and concepts 探索 NuGet v3 库,第 1 部分 介绍和概念

Exploring the NuGet v3 Libraries, Part 2 探索 NuGet v3 库,第 2 部分

Exploring the NuGet v3 Libraries, Part 3 探索 NuGet v3 库,第 3 部分

And of course, you can always go spelunking through NuGet's source code to find more examples.当然,您可以随时通过 NuGet 的源代码进行探索以找到更多示例。 Most of the core logic lives in https://github.com/nuget/nuget.client .大多数核心逻辑位于https://github.com/nuget/nuget.client 中

Best way to achieve it is by referring NugetDownloader Nuget package in your Project and use it to download any other package programmatically实现它的最佳方法是在您的项目中引用NugetDownloader Nuget 包,并使用它以编程方式下载任何其他包

Install-Package NugetDownloader

NuGet 徽章

Source code and help guide on the same is available at : https://github.com/paraspatidar/NugetDownloader源代码和帮助指南可在以下网址获得: 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示例客户端也可在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.或者,如果您想从头开始构建 Nugetdownloader 引擎,那么您也可以参考https://github.com/Azure/azure-functions-host/blob/dev/src/WebJobs.Script/Description/DotNet/PackageManager .cs因为它有一些类似的实现,但是代码理解和提取太多了。

I've been looking to do this for a while too and found a Microsoft guide on how to do exactly that.我也一直在寻求这样做,并找到了有关如何做到这一点的 Microsoft 指南。 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.这适用于 .NET Core(在 3.1 上测试)并使用 Nuget v3。

The guide: https://docs.microsoft.com/en-us/nuget/reference/nuget-client-sdk指南: https : //docs.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);

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

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