【问题标题】:How can I get the NuGet package version programmatically from a NuGet feed?如何以编程方式从 NuGet 源获取 NuGet 包版本?
【发布时间】:2019-01-11 10:40:21
【问题描述】:

首先,在所有 NuGet 代码中,我试图找出要引用的代码。

主要问题是,给定一个 NuGet 包名称,是否有一种编程方式可以从 NuGet 提要中检索版本以及用于一般消费的最新版本?

例如,给定包名称 ILMerge,最好获取最新的包版本 2.13.307。

// Pseudo code, makes a lot of assumptions about NuGet programmatic interfaces
PackageRef currentVersion = nugetlib.getpackageinfo(args[0]);
Console.WriteLine("Package Id: '{0}':", pkg.Id);
Console.WriteLine("  Current version: {0}", pkg.Version);
Console.WriteLine("  Available versions: {0}", String.Join(",",pkg.Versions.Select(_=>_)));

【问题讨论】:

标签: c# nuget


【解决方案1】:

使用NuGet core package:

string packageID = "ILMerge";

// Connect to the official package repository
IPackageRepository repo = PackageRepositoryFactory.Default.CreateRepository("https://packages.nuget.org/api/v2");
var version =repo.FindPackagesById(packageID).Max(p=>p.Version);

参考:Play with Packages, programmatically!

【讨论】:

    【解决方案2】:

    如NuGet2 所述,Nuget.Core 适用于 NuGet 版本 2。

    NuGet 客户端库的版本 3 已移至 Nuget.Client。有关详细信息,请参阅 NuGet API v3 文档。

    NuGet Client SDK

    【讨论】:

      【解决方案3】:

      有一个相当不错的 NuGet API 可以实现这两者。

      a) 检索 NuGet 包版本(针对以下示例中的 Newtonsoft.Json 包):

      GET https://api.nuget.org/v3-flatcontainer/Newtonsoft.Json/index.json
      

      b) 下载特定版本的 NuGet 包 - 例如

      GET https://api.nuget.org/v3-flatcontainer/utf8json/1.3.7/utf8json.1.3.7.nupkg
      

      请尝试将 URL 复制到浏览器,您可以看到示例的结果...

      要获取有关 API 的更多信息,请访问 Package Content

      以下代码示例可以读取包的版本(使用 Microsoft.AspNet.WebApi.Client NuGet 包,该包提供 HttpContent 的 ReadAsAsync 扩展,用于将 JSON 结果解析到适当的类 - 本示例中的 VersionsResponse)

      using System;
      using System.Net.Http;
      using System.Threading.Tasks;
      namespace GetNuGetVer
      {
          class VersionsResponse
          {
              public string[] Versions { get; set; }
          }
      
          class Program
          {
              static async Task Main(string[] args)
              {
                  var packageName = "Enums.NET";
                  var url = $"https://api.nuget.org/v3-flatcontainer/{packageName}/index.json";
                  var httpClient = new HttpClient();
                  var response = await httpClient.GetAsync(url);
                  var versionsResponse = await response.Content.ReadAsAsync<VersionsResponse>();
                  var lastVersion = versionsResponse.Versions[^1]; //(length-1)
                  // And so on ..
              }
          }
      }
      

      为了摆脱Microsoft.AspNet.WebApi.Client(依赖于Newtonsoft.Json),System.Text.Json 可以开箱即用(从 .NET Core 3.0 开始),代码更改如下:

              using System.Text.Json;
              ...
              var response = await httpClient.GetAsync(url);
              var versionsResponseBytes = await response.Content.ReadAsByteArrayAsync();
              var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
              var versionsResponse = JsonSerializer.Deserialize<VersionsResponse>(versionsResponseBytes, options);
              var lastVersion = versionsResponse.Versions[^1]; //(length-1)
      

      或者只是根据您自己的偏好使用不同的 JSON 解析器。

      【讨论】:

      猜你喜欢
      • 2016-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多