How to use IVsPackageSourceProvider provider

459 views Asked by At

How to use IVsPackageSourceProvider can I get a list of NuGet sources, like a https://www.nuget.org/api/v2/? I try to use just such a structure:

   using (Microsoft.VisualStudio.Shell.ServiceProvider provider = new Microsoft.VisualStudio.Shell.ServiceProvider((Microsoft.VisualStudio.OLE.Interop.IServiceProvider)base._dteObject))
    {

        IComponentModel service = (IComponentModel)provider.GetService(typeof(SComponentModel));
        try
        {
            var sourceProvider = service.GetService<IVsPackageSourceProvider>();
        }
        catch(Exception ex)
        {
        }
    }

But Try block raises an Exception with the text:

Cannot cast the underlying exported value of type 'NuGet.VisualStudio.VsPackageSourceProvider (ContractName="NuGet.VisualStudio.IVsPackageSourceProvider")' to type 'NuGet.VisualStudio.IVsPackageSourceProvider'

Please tell me how to use this provider.

Sry for bad english

1

There are 1 answers

3
Zhanglong Wu - MSFT On BEST ANSWER

You can get the list of packages NuGet via IVsPackageInstallerServices, like this:

try
            {
                //Using the IComponentModel service
                var componentModel = (IComponentModel)this.ServiceProvider.GetService(typeof(SComponentModel));

                //var sourceProvider = componentModel.GetService<IVsPackageSourceProvider>();

                IVsPackageInstallerServices installerServices =
                    componentModel.GetService<IVsPackageInstallerServices>();


                var installedPackages = installerServices.GetInstalledPackages();



            }
            catch (Exception ex)
            {
            }

Edit:

enter image description here

Code: https://1drv.ms/u/s!AlvaNEnglADDe3a-mSTWubSix-w