如何使用IVsPackageSourceProvider提供程序

时间:2016-12-19 15:05:35

标签: c# .net visual-studio-2012 nuget visual-studio-extensions

如何使用IVsPackageSourceProvider我可以获得一个NuGet源列表,例如https://www.nuget.org/api/v2/吗?我尝试使用这样的结构:

   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)
        {
        }
    }

但是Try阻止了Exception的文字:

  

无法转换类型的基础导出值   &#39; NuGet.VisualStudio.VsPackageSourceProvider   (ContractName =&#34; NuGet.VisualStudio.IVsPackageSourceProvider&#34)&#39;输入   &#39; NuGet.VisualStudio.IVsPackageSourceProvider&#39;

请告诉我如何使用此提供商。

Sry for bad english

1 个答案:

答案 0 :(得分:0)

您可以通过IVsPackageInstallerServices获取NuGet包列表,如下所示:

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)
            {
            }

编辑:

enter image description here

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