diff --git a/src/VisualStudio/Core/Def/Implementation/LanguageService/AbstractPackage.cs b/src/VisualStudio/Core/Def/Implementation/LanguageService/AbstractPackage.cs index 2e697d6d82423..654e8721e475e 100644 --- a/src/VisualStudio/Core/Def/Implementation/LanguageService/AbstractPackage.cs +++ b/src/VisualStudio/Core/Def/Implementation/LanguageService/AbstractPackage.cs @@ -2,9 +2,9 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - +using System; using System.Threading; +using Microsoft.VisualStudio.ComponentModelHost; using Microsoft.VisualStudio.Shell; using Task = System.Threading.Tasks.Task; @@ -12,6 +12,26 @@ namespace Microsoft.VisualStudio.LanguageServices.Implementation.LanguageService { internal abstract class AbstractPackage : AsyncPackage { + private IComponentModel? _componentModel_doNotAccessDirectly; + + internal IComponentModel ComponentModel + { + get + { + Assumes.Present(_componentModel_doNotAccessDirectly); + return _componentModel_doNotAccessDirectly; + } + } + + protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress progress) + { + await base.InitializeAsync(cancellationToken, progress).ConfigureAwait(true); + await JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken); + + _componentModel_doNotAccessDirectly = (IComponentModel)await GetServiceAsync(typeof(SComponentModel)).ConfigureAwait(true); + Assumes.Present(_componentModel_doNotAccessDirectly); + } + protected async Task LoadComponentsInUIContextOnceSolutionFullyLoadedAsync(CancellationToken cancellationToken) { // UIContexts can be "zombied" if UIContexts aren't supported because we're in a command line build or in other scenarios. diff --git a/src/VisualStudio/Core/Def/Implementation/LanguageService/AbstractPackage`2.cs b/src/VisualStudio/Core/Def/Implementation/LanguageService/AbstractPackage`2.cs index d238894e8d293..f7546e274dc53 100644 --- a/src/VisualStudio/Core/Def/Implementation/LanguageService/AbstractPackage`2.cs +++ b/src/VisualStudio/Core/Def/Implementation/LanguageService/AbstractPackage`2.cs @@ -32,7 +32,6 @@ internal abstract partial class AbstractPackage : Ab private PackageInstallerService _packageInstallerService; private VisualStudioSymbolSearchService _symbolSearchService; - private IComponentModel _componentModel_doNotAccessDirectly; protected AbstractPackage() { @@ -44,11 +43,9 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke await JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken); - _componentModel_doNotAccessDirectly = (IComponentModel)await GetServiceAsync(typeof(SComponentModel)).ConfigureAwait(true); var shell = (IVsShell7)await GetServiceAsync(typeof(SVsShell)).ConfigureAwait(true); var solution = (IVsSolution)await GetServiceAsync(typeof(SVsSolution)).ConfigureAwait(true); cancellationToken.ThrowIfCancellationRequested(); - Assumes.Present(_componentModel_doNotAccessDirectly); Assumes.Present(shell); Assumes.Present(solution); @@ -105,15 +102,6 @@ protected override async Task LoadComponentsAsync(CancellationToken cancellation _symbolSearchService?.Connect(this.RoslynLanguageName); } - internal IComponentModel ComponentModel - { - get - { - Assumes.Present(_componentModel_doNotAccessDirectly); - return _componentModel_doNotAccessDirectly; - } - } - protected abstract void RegisterMiscellaneousFilesWorkspaceInformation(MiscellaneousFilesWorkspace miscellaneousFilesWorkspace); protected abstract IEnumerable CreateEditorFactories(); diff --git a/src/VisualStudio/Core/Def/RoslynPackage.cs b/src/VisualStudio/Core/Def/RoslynPackage.cs index 5890c9ad8e5d6..5cfd84c835325 100644 --- a/src/VisualStudio/Core/Def/RoslynPackage.cs +++ b/src/VisualStudio/Core/Def/RoslynPackage.cs @@ -64,7 +64,6 @@ internal sealed class RoslynPackage : AbstractPackage private static RoslynPackage? _lazyInstance; private VisualStudioWorkspace? _workspace; - private IComponentModel? _componentModel; private RuleSetEventHandler? _ruleSetEventHandler; private ColorSchemeApplier? _colorSchemeApplier; private IDisposable? _solutionEventMonitor; @@ -151,14 +150,12 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke await JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken); - _componentModel = (IComponentModel)await GetServiceAsync(typeof(SComponentModel)).ConfigureAwait(true); cancellationToken.ThrowIfCancellationRequested(); - Assumes.Present(_componentModel); // Ensure the options persisters are loaded since we have to fetch options from the shell - LoadOptionPersistersAsync(_componentModel, cancellationToken).Forget(); + LoadOptionPersistersAsync(this.ComponentModel, cancellationToken).Forget(); - _workspace = _componentModel.GetService(); + _workspace = this.ComponentModel.GetService(); // Fetch the session synchronously on the UI thread; if this doesn't happen before we try using this on // the background thread then we will experience hangs like we see in this bug: @@ -179,7 +176,7 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke TrackBulkFileOperations(); - var settingsEditorFactory = _componentModel.GetService(); + var settingsEditorFactory = this.ComponentModel.GetService(); RegisterEditorFactory(settingsEditorFactory); } @@ -304,14 +301,6 @@ private async Task LoadCallstackExplorerMenusAsync(CancellationToken cancellatio StackTraceExplorerCommandHandler.Initialize(menuCommandService, this); } - internal IComponentModel ComponentModel - { - get - { - return _componentModel ?? throw new InvalidOperationException($"Cannot use {nameof(RoslynPackage)}.{nameof(ComponentModel)} prior to initialization."); - } - } - protected override void Dispose(bool disposing) { DisposeVisualStudioServices();