簡體   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