Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid hitting the UI thread when retrieving MEF specific services #4229

Merged
merged 6 commits into from
Sep 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/NuGet.Clients/NuGet.Console/PowerConsoleToolWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ private IOutputConsole VSOutputConsole
{
_vsOutputConsole = NuGetUIThreadHelper.JoinableTaskFactory.Run(async () =>
{
var consoleProvider = await ServiceLocator.GetInstanceAsync<IOutputConsoleProvider>();
var consoleProvider = await ServiceLocator.GetComponentModelServiceAsync<IOutputConsoleProvider>();
if (null != consoleProvider)
{
return await consoleProvider.CreatePackageManagerConsoleAsync();
Expand All @@ -703,7 +703,7 @@ private IConsoleStatus ConsoleStatus
{
if (_consoleStatus == null)
{
_consoleStatus = ServiceLocator.GetInstance<IConsoleStatus>();
_consoleStatus = ServiceLocator.GetComponentModelService<IConsoleStatus>();
Debug.Assert(_consoleStatus != null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ await System.Threading.Tasks.Task.Run(

Assumes.NotNull(_solutionManager);

var productUpdateService = ServiceLocator.GetInstance<IProductUpdateService>();
var packageRestoreManager = ServiceLocator.GetInstance<IPackageRestoreManager>();
var deleteOnRestartManager = ServiceLocator.GetInstance<IDeleteOnRestartManager>();
var productUpdateService = ServiceLocator.GetComponentModelService<IProductUpdateService>();
var packageRestoreManager = ServiceLocator.GetComponentModelService<IPackageRestoreManager>();
var deleteOnRestartManager = ServiceLocator.GetComponentModelService<IDeleteOnRestartManager>();
var shell = ServiceLocator.GetGlobalService<SVsShell, IVsShell4>();

await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public class UninstallPackageCommand : NuGetPowerShellBaseCommand

public UninstallPackageCommand()
{
_deleteOnRestartManager = ServiceLocator.GetInstance<IDeleteOnRestartManager>();
_lockService = ServiceLocator.GetInstance<INuGetLockService>();
_deleteOnRestartManager = ServiceLocator.GetComponentModelService<IDeleteOnRestartManager>();
_lockService = ServiceLocator.GetComponentModelService<INuGetLockService>();
}

[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, Position = 0)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ public abstract class NuGetPowerShellBaseCommand : PSCmdlet, IPSNuGetProjectCont

protected NuGetPowerShellBaseCommand()
{
_sourceRepositoryProvider = ServiceLocator.GetInstance<ISourceRepositoryProvider>();
ConfigSettings = ServiceLocator.GetInstance<Configuration.ISettings>();
VsSolutionManager = ServiceLocator.GetInstance<IVsSolutionManager>();
_sourceRepositoryProvider = ServiceLocator.GetComponentModelService<ISourceRepositoryProvider>();
ConfigSettings = ServiceLocator.GetComponentModelService<Configuration.ISettings>();
VsSolutionManager = ServiceLocator.GetComponentModelService<IVsSolutionManager>();
DTE = ServiceLocator.GetInstance<DTE>();
SourceControlManagerProvider = ServiceLocator.GetInstance<ISourceControlManagerProvider>();
_commonOperations = ServiceLocator.GetInstance<ICommonOperations>();
PackageRestoreManager = ServiceLocator.GetInstance<IPackageRestoreManager>();
_deleteOnRestartManager = ServiceLocator.GetInstance<IDeleteOnRestartManager>();
SourceControlManagerProvider = ServiceLocator.GetComponentModelService<ISourceControlManagerProvider>();
_commonOperations = ServiceLocator.GetComponentModelService<ICommonOperations>();
PackageRestoreManager = ServiceLocator.GetComponentModelService<IPackageRestoreManager>();
_deleteOnRestartManager = ServiceLocator.GetComponentModelService<IDeleteOnRestartManager>();

var logger = new LoggerAdapter(this);
PackageExtractionContext = new PackageExtractionContext(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public class PackageActionBaseCommand : NuGetPowerShellBaseCommand

public PackageActionBaseCommand()
{
_deleteOnRestartManager = ServiceLocator.GetInstance<IDeleteOnRestartManager>();
_lockService = ServiceLocator.GetInstance<INuGetLockService>();
_deleteOnRestartManager = ServiceLocator.GetComponentModelService<IDeleteOnRestartManager>();
_lockService = ServiceLocator.GetComponentModelService<INuGetLockService>();
}

[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, Position = 0)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public partial class GeneralOptionControl : UserControl
public GeneralOptionControl()
{
InitializeComponent();
_settings = ServiceLocator.GetInstance<Configuration.ISettings>();
_outputConsoleLogger = ServiceLocator.GetInstance<INuGetUILogger>();
_settings = ServiceLocator.GetComponentModelService<ISettings>();
_outputConsoleLogger = ServiceLocator.GetComponentModelService<INuGetUILogger>();
_localsCommandRunner = new LocalsCommandRunner();
AutoScroll = true;
Debug.Assert(_settings != null);
Expand Down Expand Up @@ -150,7 +150,7 @@ private void OnLocalsCommandButtonOnClick(object sender, EventArgs e)
{
UpdateLocalsCommandStatusText(string.Format(Resources.ShowMessage_LocalsCommandWorking), visibility: true);
var arguments = new List<string> { "all" };
var settings = ServiceLocator.GetInstance<ISettings>();
var settings = ServiceLocator.GetComponentModelService<ISettings>();
var logError = new LocalsArgs.Log(LogError);
var logInformation = new LocalsArgs.Log(LogInformation);
var localsArgs = new LocalsArgs(arguments, settings, logInformation, logError, clear: true, list: false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private void OnTimerTick(object sender, EventArgs e)
timer.Stop();
timer.Dispose();

var optionsPageActivator = ServiceLocator.GetInstance<IOptionsPageActivator>();
var optionsPageActivator = ServiceLocator.GetComponentModelService<IOptionsPageActivator>();
if (optionsPageActivator != null)
{
optionsPageActivator.NotifyOptionsDialogClosed();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private async ValueTask InitializeAsync(PackageManagerModel model, INuGetUILogge

Model = model;
_uiLogger = uiLogger;
Settings = await ServiceLocator.GetInstanceAsync<ISettings>();
Settings = await ServiceLocator.GetComponentModelServiceAsync<ISettings>();

_windowSearchHostFactory = await ServiceLocator.GetGlobalServiceAsync<SVsWindowSearchHostFactory, IVsWindowSearchHostFactory>();
_serviceBroker = model.Context.ServiceBroker;
Expand Down Expand Up @@ -882,7 +882,7 @@ private async Task<bool> GetUseRecommendedPackagesAsync(PackageLoadContext loadC
_recommendPackages = true;
}

NuGetExperimentationService = await ServiceLocator.GetInstanceAsync<INuGetExperimentationService>();
NuGetExperimentationService = await ServiceLocator.GetComponentModelServiceAsync<INuGetExperimentationService>();
// Check for A/B experiment here. For control group, return false instead of _recommendPackages
if (IsRecommenderFlightEnabled(NuGetExperimentationService))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ private async ValueTask<IInstalledAndTransitivePackages> GetInstalledAndTransiti
CancellationToken cancellationToken)
{
var logger = new VisualStudioActivityLogger();
var uiLogger = ServiceLocator.GetInstance<INuGetUILogger>();
var uiLogger = await ServiceLocator.GetComponentModelServiceAsync<INuGetUILogger>();
var packageFeeds = (mainFeed: (IPackageFeed?)null, recommenderFeed: (IPackageFeed?)null);

if (itemFilter == ItemFilter.All && recommendPackages == false)
Expand Down Expand Up @@ -414,7 +414,7 @@ private async Task<IReadOnlyList<SourceRepository>> GetGlobalPackageFolderReposi
private async Task<SourceRepository> GetPackagesFolderSourceRepositoryAsync()
{
IVsSolutionManager solutionManager = await _sharedServiceState.SolutionManager.GetValueAsync();
ISettings settings = ServiceLocator.GetInstance<ISettings>();
ISettings settings = await ServiceLocator.GetComponentModelServiceAsync<ISettings>();

return _sharedServiceState.SourceRepositoryProvider.CreateRepository(
new PackageSource(PackagesFolderPathUtility.GetPackagesFolderPath(solutionManager, settings)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ await CatchAndRethrowExceptionAsync(async () =>

try
{
projectContext = await ServiceLocator.GetInstanceAsync<INuGetProjectContext>();
projectContext = await ServiceLocator.GetComponentModelServiceAsync<INuGetProjectContext>();

Assumes.NotNull(projectContext);

Expand Down Expand Up @@ -418,7 +418,7 @@ public async ValueTask<IReadOnlyList<ProjectAction>> GetInstallActionsAsync(

Assumes.NotNullOrEmpty(sourceRepositories);

INuGetProjectContext projectContext = await ServiceLocator.GetInstanceAsync<INuGetProjectContext>();
INuGetProjectContext projectContext = await ServiceLocator.GetComponentModelServiceAsync<INuGetProjectContext>();
IReadOnlyList<NuGetProject> projects = await GetProjectsAsync(projectIds, cancellationToken);

var resolutionContext = new ResolutionContext(
Expand Down Expand Up @@ -471,7 +471,7 @@ public async ValueTask<IReadOnlyList<ProjectAction>> GetUninstallActionsAsync(

return await CatchAndRethrowExceptionAsync(async () =>
{
INuGetProjectContext projectContext = await ServiceLocator.GetInstanceAsync<INuGetProjectContext>();
INuGetProjectContext projectContext = await ServiceLocator.GetComponentModelServiceAsync<INuGetProjectContext>();
IReadOnlyList<NuGetProject> projects = await GetProjectsAsync(projectIds, cancellationToken);

var projectActions = new List<ProjectAction>();
Expand Down Expand Up @@ -541,7 +541,7 @@ public async ValueTask<IReadOnlyList<ProjectAction>> GetUpdateActionsAsync(
}
}

INuGetProjectContext projectContext = await ServiceLocator.GetInstanceAsync<INuGetProjectContext>();
INuGetProjectContext projectContext = await ServiceLocator.GetComponentModelServiceAsync<INuGetProjectContext>();
IReadOnlyList<NuGetProject> projects = await GetProjectsAsync(projectIds, cancellationToken);

var resolutionContext = new ResolutionContext(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public async ValueTask UninstallPackagesAsync(
NuGetPackageManager packageManager = await _state.GetPackageManagerAsync(cancellationToken);
Assumes.NotNull(packageManager);

INuGetProjectContext projectContext = await ServiceLocator.GetInstanceAsync<INuGetProjectContext>();
INuGetProjectContext projectContext = await ServiceLocator.GetComponentModelServiceAsync<INuGetProjectContext>();
Assumes.NotNull(projectContext);

await packageManager.ExecuteNuGetProjectActionsAsync(
Expand Down Expand Up @@ -186,7 +186,7 @@ public async ValueTask InstallPackagesAsync(
NuGetPackageManager packageManager = await _state.GetPackageManagerAsync(cancellationToken);
Assumes.NotNull(packageManager);

INuGetProjectContext projectContext = await ServiceLocator.GetInstanceAsync<INuGetProjectContext>();
INuGetProjectContext projectContext = await ServiceLocator.GetComponentModelServiceAsync<INuGetProjectContext>();
Assumes.NotNull(projectContext);

await packageManager.ExecuteBuildIntegratedProjectActionsAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal SharedServiceState(ISourceRepositoryProvider sourceRepositoryProvider)
SourceRepositoryProvider.PackageSourceProvider.PackageSourcesChanged += PackageSourcesChanged;

SolutionManager = new AsyncLazy<IVsSolutionManager>(
ServiceLocator.GetInstanceAsync<IVsSolutionManager>,
ServiceLocator.GetComponentModelServiceAsync<IVsSolutionManager>,
NuGetUIThreadHelper.JoinableTaskFactory);

SourceRepositories = new AsyncLazy<IReadOnlyCollection<SourceRepository>>(
Expand All @@ -41,15 +41,15 @@ internal SharedServiceState(ISourceRepositoryProvider sourceRepositoryProvider)

public static async ValueTask<SharedServiceState> CreateAsync(CancellationToken cancellationToken)
{
var sourceRepositoryProvider = await ServiceLocator.GetInstanceAsync<ISourceRepositoryProvider>();
var sourceRepositoryProvider = await ServiceLocator.GetComponentModelServiceAsync<ISourceRepositoryProvider>();

return new SharedServiceState(sourceRepositoryProvider);
}

public async ValueTask<NuGetPackageManager> GetPackageManagerAsync(CancellationToken cancellationToken)
{
IDeleteOnRestartManager deleteOnRestartManager = await ServiceLocator.GetInstanceAsync<IDeleteOnRestartManager>();
ISettings settings = await ServiceLocator.GetInstanceAsync<ISettings>();
IDeleteOnRestartManager deleteOnRestartManager = await ServiceLocator.GetComponentModelServiceAsync<IDeleteOnRestartManager>();
ISettings settings = await ServiceLocator.GetComponentModelServiceAsync<ISettings>();
IVsSolutionManager solutionManager = await SolutionManager.GetValueAsync(cancellationToken);

return new NuGetPackageManager(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public static async Task<bool> IsNuGetProjectUpgradeableAsync(NuGetProject nuGet

private static async Task<NuGetProject> GetNuGetProject(Project envDTEProject)
{
var solutionManager = ServiceLocator.GetInstance<IVsSolutionManager>();
var solutionManager = await ServiceLocator.GetComponentModelServiceAsync<IVsSolutionManager>();

var projectSafeName = await envDTEProject.GetCustomUniqueNameAsync();
var nuGetProject = await solutionManager.GetNuGetProjectAsync(projectSafeName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static async Task<IList<PackageIdentity>> GetPackagesToBeReinstalled(NuGe
if (installedRefs != null && installedRefs.Any())
{
var targetFramework = project.GetMetadata<NuGetFramework>(NuGetProjectMetadataKeys.TargetFramework);
return GetPackagesToBeReinstalled(targetFramework, installedRefs);
return await GetPackagesToBeReinstalledAsync(targetFramework, installedRefs);
}

return new List<PackageIdentity>();
Expand All @@ -55,16 +55,16 @@ public static async Task<IList<PackageIdentity>> GetPackagesToBeReinstalled(NuGe
/// <param name="projectFramework">Current target framework of the project</param>
/// <param name="packageReferences">List of package references in the project from which packages to be reinstalled are determined</param>
/// <returns>List of package identities to be reinstalled</returns>
public static List<PackageIdentity> GetPackagesToBeReinstalled(NuGetFramework projectFramework, IEnumerable<Packaging.PackageReference> packageReferences)
public static async Task<List<PackageIdentity>> GetPackagesToBeReinstalledAsync(NuGetFramework projectFramework, IEnumerable<Packaging.PackageReference> packageReferences)
{
Debug.Assert(projectFramework != null);
Debug.Assert(packageReferences != null);

var packagesToBeReinstalled = new List<PackageIdentity>();
var sourceRepositoryProvider = ServiceLocator.GetInstance<ISourceRepositoryProvider>();
var solutionManager = ServiceLocator.GetInstance<ISolutionManager>();
var settings = ServiceLocator.GetInstance<Configuration.ISettings>();
var deleteOnRestartManager = ServiceLocator.GetInstance<IDeleteOnRestartManager>();
var sourceRepositoryProvider = await ServiceLocator.GetComponentModelServiceAsync<ISourceRepositoryProvider>();
var solutionManager = await ServiceLocator.GetComponentModelServiceAsync<ISolutionManager>();
var settings = await ServiceLocator.GetComponentModelServiceAsync<Configuration.ISettings>();
var deleteOnRestartManager = await ServiceLocator.GetComponentModelServiceAsync<IDeleteOnRestartManager>();
var packageManager = new NuGetPackageManager(
sourceRepositoryProvider,
settings,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static class SettingsHelper
/// </param>
public static void Set(string property, string value)
{
var settings = ServiceLocator.GetInstance<Configuration.ISettings>();
var settings = ServiceLocator.GetComponentModelService<ISettings>();
var packageRestoreConsent = new PackageRestoreConsent(settings);
if (string.Equals(property, "PackageRestoreConsentGranted", StringComparison.OrdinalIgnoreCase))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,11 @@ private async ValueTask<object> CreatePackageFileServiceAsync(
}
private async Task InitializeAsync()
{
_lazySettings = new AsyncLazy<ISettings>(ServiceLocator.GetInstanceAsync<ISettings>, ThreadHelper.JoinableTaskFactory);
_lazySolutionManager = new AsyncLazy<IVsSolutionManager>(ServiceLocator.GetInstanceAsync<IVsSolutionManager>, ThreadHelper.JoinableTaskFactory);
_lazySettings = new AsyncLazy<ISettings>(ServiceLocator.GetComponentModelServiceAsync<ISettings>, ThreadHelper.JoinableTaskFactory);
_lazySolutionManager = new AsyncLazy<IVsSolutionManager>(ServiceLocator.GetComponentModelServiceAsync<IVsSolutionManager>, ThreadHelper.JoinableTaskFactory);
_projectManagerServiceSharedState = new NuGetProjectManagerServiceState();
_sharedServiceState = await SharedServiceState.CreateAsync(CancellationToken.None);
_lazyTelemetryProvider = new AsyncLazy<INuGetTelemetryProvider>(ServiceLocator.GetInstanceAsync<INuGetTelemetryProvider>, ThreadHelper.JoinableTaskFactory);
_lazyTelemetryProvider = new AsyncLazy<INuGetTelemetryProvider>(ServiceLocator.GetComponentModelServiceAsync<INuGetTelemetryProvider>, ThreadHelper.JoinableTaskFactory);
}
}
}
Loading