Пример #1
0
        public static async ValueTask <IReadOnlyCollection <PackageSourceMoniker> > PopulateListAsync(
            IServiceBroker serviceBroker,
            CancellationToken cancellationToken)
        {
            Assumes.NotNull(serviceBroker);

            using (INuGetSourcesService nugetSourcesService = await serviceBroker.GetProxyAsync <INuGetSourcesService>(
                       NuGetServices.SourceProviderService,
                       cancellationToken))
            {
                Assumes.NotNull(nugetSourcesService);
                IReadOnlyList <PackageSourceContextInfo> packageSources = await nugetSourcesService.GetPackageSourcesAsync(cancellationToken);

                return(await PopulateListAsync(packageSources, cancellationToken));
            }
        }
        internal async Task InitializeOnActivatedAsync(CancellationToken cancellationToken)
        {
            try
            {
                if (_initialized)
                {
                    return;
                }

                _initialized = true;

                IServiceBrokerProvider serviceBrokerProvider = await ServiceLocator.GetComponentModelServiceAsync <IServiceBrokerProvider>();

                IServiceBroker serviceBroker = await serviceBrokerProvider.GetAsync();

#pragma warning disable ISB001 // Dispose of proxies, disposed in disposing event or in ClearSettings
                _nugetSourcesService = await serviceBroker.GetProxyAsync <INuGetSourcesService>(
                    NuGetServices.SourceProviderService,
                    cancellationToken : cancellationToken);

#pragma warning restore ISB001 // Dispose of proxies, disposed in disposing event or in ClearSettings
                Assumes.NotNull(_nugetSourcesService);

                // get packages sources
                _originalPackageSources = await _nugetSourcesService.GetPackageSourcesAsync(cancellationToken);

                // packageSources and machineWidePackageSources are deep cloned when created, no need to worry about re-querying for sources to diff changes
                var allPackageSources         = _originalPackageSources;
                var packageSources            = allPackageSources.Where(ps => !ps.IsMachineWide).ToList();
                var machineWidePackageSources = allPackageSources.Where(ps => ps.IsMachineWide).ToList();

                // bind to the package sources, excluding Aggregate
                _packageSources = new BindingSource(packageSources.Select(ps => ps.Clone()).ToList(), null);
                _packageSources.CurrentChanged  += OnSelectedPackageSourceChanged;
                PackageSourcesListBox.GotFocus  += PackageSourcesListBox_GotFocus;
                PackageSourcesListBox.DataSource = _packageSources;
                ResetItemsCheckedState(PackageSourcesListBox, _packageSources);

                if (machineWidePackageSources.Count > 0)
                {
                    _machineWidepackageSources = new BindingSource(machineWidePackageSources.Select(ps => ps.Clone()).ToList(), null);
                    _machineWidepackageSources.CurrentChanged  += OnSelectedMachineWidePackageSourceChanged;
                    MachineWidePackageSourcesListBox.GotFocus  += MachineWidePackageSourcesListBox_GotFocus;
                    MachineWidePackageSourcesListBox.DataSource = _machineWidepackageSources;
                    ResetItemsCheckedState(MachineWidePackageSourcesListBox, _machineWidepackageSources);
                }
                else
                {
                    MachineWidePackageSourcesListBox.Visible = MachineWideSourcesLabel.Visible = false;
                }

                OnSelectedPackageSourceChanged(null, EventArgs.Empty);
            }
            // Thrown during creating or saving NuGet.Config.
            catch (NuGetConfigurationException ex)
            {
                MessageHelper.ShowErrorMessage(ex.Message, Resources.ErrorDialogBoxTitle);
            }
            // Thrown if no nuget.config found.
            catch (InvalidOperationException ex)
            {
                MessageHelper.ShowErrorMessage(ex.Message, Resources.ErrorDialogBoxTitle);
            }
            catch (UnauthorizedAccessException)
            {
                MessageHelper.ShowErrorMessage(Resources.ShowError_ConfigUnauthorizedAccess, Resources.ErrorDialogBoxTitle);
            }
            // Unknown exception.
            catch (Exception ex)
            {
                MessageHelper.ShowErrorMessage(Resources.ShowError_SettingActivatedFailed, Resources.ErrorDialogBoxTitle);
                ActivityLog.LogError(NuGetUI.LogEntrySource, ex.ToString());
            }
        }