从 URL 指定的远程存储库获取 NuGet 包版本而不使用 NuGet.Core

Get NuGet package version from remote repository specified by URL without using NuGet.Core

NuGet.Core 已被弃用,我正在努力寻找 API 以从 URL 指定的远程存储库获取 NuGet 包版本。使用 IPackageRepository 非常简单,但如果不使用 NuGet.Core?

我不知道如何去做

编辑:FactoryExtensionsV3.GetCoreV3 可能指的是 "NuGet version 3",而不是 NuGet 的 v3 HTTP API。因此,此代码适用于本地文件提要以及 V2 和 V3 HTTP 提要。只需更改传递给 PackageSource 的 URL。

static async Task Main(string[] args)
{
    var packageSource = new PackageSource("https://api.nuget.org/v3/index.json");
    var sourceRepository = new SourceRepository(packageSource, FactoryExtensionsV3.GetCoreV3(null));

    var metadata = await sourceRepository.GetResourceAsync<MetadataResource>();
    var cacheContext = new SourceCacheContext();
    var versions = await metadata.GetVersions("newtonsoft.json", cacheContext, NullLogger.Instance, CancellationToken.None);

    foreach (var version in versions)
    {
        Console.WriteLine("Found version " + version.ToNormalizedString());
    }
}

我制作了一个包,以便更轻松地与 NuGet V3 服务器进行交互:https://github.com/loic-sharma/BaGet/tree/master/src/BaGet.Protocol#list-package-versions

NuGetClient client = new NuGetClient("https://api.nuget.org/v3/index.json");

IReadOnlyList<NuGetVersion>> versions = await client.ListPackageVersionsAsync("Newtonsoft.Json");

foreach (NuGetVersion version in versions)
{
    Console.WriteLine($"Found version: {version}");
}

如果您有任何反馈或建议,请告诉我! :)