public async Task <NuGetInstalledPackage> GetInstalledPackageAsync(string projectName, string packageName)
        {
            var solution = VisualStudioObjectProviders.GetService <SVsSolution, IVsSolution>();
            int result   = solution.GetProjectOfUniqueName(projectName, out IVsHierarchy project);

            if (result != VSConstants.S_OK)
            {
                throw new Exception($"Error calling {nameof(IVsSolution)}.{nameof(IVsSolution.GetProjectOfUniqueName)}: {result}");
            }

            result = solution.GetGuidOfProject(project, out Guid projectGuid);
            if (result != VSConstants.S_OK)
            {
                throw new Exception($"Error calling {nameof(IVsSolution)}.{nameof(IVsSolution.GetGuidOfProject)}: {result}");
            }

            var serviceBrokerContainer = VisualStudioObjectProviders.GetService <SVsBrokeredServiceContainer, IBrokeredServiceContainer>();
            var serviceBroker          = serviceBrokerContainer.GetFullAccessServiceBroker();

            INuGetProjectService projectService = await serviceBroker.GetProxyAsync <INuGetProjectService>(NuGetServices.NuGetProjectServiceV1);

            using (projectService as IDisposable)
            {
                var packagesResult = await projectService.GetInstalledPackagesAsync(projectGuid, CancellationToken.None);

                if (packagesResult.Status != InstalledPackageResultStatus.Successful)
                {
                    throw new Exception("Unexpected result from GetInstalledPackagesAsync: " + packagesResult.Status);
                }

                return(packagesResult.Packages
                       .Where(p => p.DirectDependency)
                       .FirstOrDefault(p => StringComparer.OrdinalIgnoreCase.Equals(p.Id, packageName)));
            }
        }
예제 #2
0
        public IEnumerable <KeyValuePair <string, string> > GetSources(bool includeUnOfficial, bool includeDisabled)
        {
            IVsPackageSourceProvider packageSourceProvider = VisualStudioObjectProviders.GetComponentModelService <IVsPackageSourceProvider>();

            return(packageSourceProvider.GetSources(includeUnOfficial, includeDisabled));
        }