Search code examples
c#nuget.net-4.6.1

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


NuGet.Core has been deprecated, I am struggling to find out an API to get NuGet package version from remote repository specified by URL. Using IPackageRepository was quite straight forward but I am not getting any way how to do it without using NuGet.Core?


Solution

  • Edit: FactoryExtensionsV3.GetCoreV3 probably refers to "NuGet version 3", not NuGet's v3 HTTP API. So, this code works for local file feeds as well as both V2 and V3 HTTP feeds. Just change the URL passed to PackageSource.

    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());
        }
    }