nugetvisual-studio-extensionsvsixnuget-serverdotnet-cli

how to call Package Manager Console and execute Find-Package in visual studio extension


I'm developing a visual studio extension(vsix).

Now, I want to get a nuget package latest version. I only found one way by using Package Manager Console, dotnet CLI still not support search command.

So, how can I call Package Manager Console and execute Find-Package command, or is there another way can get package latest version from specific package source server.


Solution

  • Have you tried the way in below doc or not?

    https://learn.microsoft.com/en-us/nuget/guides/api/query-for-all-published-packages

    I built a simple VSIX with customer Command item, and it could use below code to perform search packages from NuGet(need to install Nuget.PackageManagement into your VSIX project first).

            List<Lazy<INuGetResourceProvider>> providers = new List<Lazy<INuGetResourceProvider>>();
            providers.AddRange(Repository.Provider.GetCoreV3());  // Add v3 API support
            //providers.AddRange(Repository.Provider.GetCoreV2());  // Add v2 API support
            PackageSource packageSource = new PackageSource("https://api.nuget.org/v3/index.json");
            SourceRepository sourceRepository = new SourceRepository(packageSource, providers);
            PackageMetadataResource packageMetadataResource = await sourceRepository.GetResourceAsync<PackageMetadataResource>();
            IEnumerable<IPackageSearchMetadata> searchMetadata = await packageMetadataResource.GetMetadataAsync("JSON.net", true, true, null, NuGet.Common.NullLogger.Instance, CancellationToken.None);
            var a= searchMetadata.ToString();