【问题标题】:How to download a nupkg package from nuget programmatically in .NET Core?如何在 .NET Core 中以编程方式从 nuget 下载 nupkg 包?
【发布时间】:2017-02-11 10:15:05
【问题描述】:

在过去的 .NET Framework 中,我使用此示例以编程方式使用 nuget

Play with Packages, programmatically!

.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

【问题讨论】:

  • 您是否尝试过使用 V3 链接作为存储库路径?
  • @MathivananKP,我试过了,但“NuGet.Core”程序集在 .NET Core 上不起作用。

标签: c# nuget .net-core


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    Install-Package NugetDownloader

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

    【讨论】:

    • 在回答中提到您是建议实用程序的作者之一是一种习惯。
    【解决方案3】:

    我也一直在寻找这样做的时间,并找到了有关如何做到这一点的 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);
    

    【讨论】:

      猜你喜欢
      • 2019-09-01
      • 1970-01-01
      • 2018-06-09
      • 1970-01-01
      • 2019-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多