Example #1
0
        /// <summary>
        /// Initialization of the package; this method is called right after the package is sited, so this is the place
        /// where you can put all the initialization code that rely on services provided by VisualStudio.
        /// </summary>
        protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress <ServiceProgressData> progress)
        {
            await base.InitializeAsync(cancellationToken, progress);

            // Add our command handlers for menu (commands must exist in the .vsct file)
            await AddMenuCommandHandlersAsync();

            // This instantiates a decoupled ICommand instance responsible to locate and display output pane by a UI control
            UI.Commands.ShowErrorsCommand = new ShowErrorsCommand(this);

            _vsMonitorSelection = new AsyncLazy <IVsMonitorSelection>(
                async() =>
            {
                await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

                // get the UI context cookie for the debugging mode
                var vsMonitorSelection = await GetServiceAsync(typeof(IVsMonitorSelection)) as IVsMonitorSelection;
                Assumes.Present(vsMonitorSelection);
                // get the solution not building and not debugging cookie
                var guidCmdUI = VSConstants.UICONTEXT.SolutionExistsAndFullyLoaded_guid;
                vsMonitorSelection.GetCmdUIContextCookie(
                    ref guidCmdUI, out _solutionExistsAndFullyLoadedContextCookie);

                guidCmdUI = VSConstants.UICONTEXT.SolutionExistsAndNotBuildingAndNotDebugging_guid;
                vsMonitorSelection.GetCmdUIContextCookie(
                    ref guidCmdUI, out _solutionNotBuildingAndNotDebuggingContextCookie);

                guidCmdUI = VSConstants.UICONTEXT.SolutionExists_guid;
                vsMonitorSelection.GetCmdUIContextCookie(
                    ref guidCmdUI, out _solutionExistsCookie);

                return(vsMonitorSelection);
            },
                ThreadHelper.JoinableTaskFactory);

            IBrokeredServiceContainer brokeredServiceContainer = await this.GetServiceAsync <SVsBrokeredServiceContainer, IBrokeredServiceContainer>();

            var lazySolutionManager         = new AsyncLazy <IVsSolutionManager>(() => ServiceLocator.GetInstanceAsync <IVsSolutionManager>(), ThreadHelper.JoinableTaskFactory);
            var lazySettings                = new AsyncLazy <ISettings>(() => ServiceLocator.GetInstanceAsync <ISettings>(), ThreadHelper.JoinableTaskFactory);
            var nuGetBrokeredServiceFactory = new NuGetBrokeredServiceFactory(lazySolutionManager, lazySettings);

            brokeredServiceContainer.Proffer(NuGetServices.NuGetProjectServiceV1, nuGetBrokeredServiceFactory.CreateNuGetProjectServiceV1);
        }
        private async Task RunSolutionRestoreAsync()
        {
            var cancellationTokenSource = new CancellationTokenSource();

            _cancelBuildToken = cancellationTokenSource;

            try
            {
                await GetAndActivatePackageManagerOutputWindowAsync();

                await TaskScheduler.Default;
                SB.IBrokeredServiceContainer serviceContainer = await _asyncServiceProvider.GetServiceAsync <SB.SVsBrokeredServiceContainer, SB.IBrokeredServiceContainer>();

                IServiceBroker serviceBroker = serviceContainer.GetFullAccessServiceBroker();

                INuGetSolutionService nugetSolutionService = await serviceBroker.GetProxyAsync <INuGetSolutionService>(NuGetServices.SolutionService);

                try
                {
                    await nugetSolutionService.RestoreSolutionAsync(cancellationTokenSource.Token);
                }
                finally
                {
                    (nugetSolutionService as IDisposable)?.Dispose();
                }
            }
            catch (Exception e)
            {
                // Only log to the activity log for now
                // TODO: https://github.com/NuGet/Home/issues/9352
                ActivityLog.LogError("NuGet Package Manager", e.Message);
            }
            finally
            {
                cancellationTokenSource.Dispose();
                _cancelBuildToken = null;
            }
        }