-
Notifications
You must be signed in to change notification settings - Fork 761
Use async service provider in ServiceLocator #2898
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ | |
| using Microsoft.VisualStudio.Shell; | ||
| using Microsoft.VisualStudio.Shell.Interop; | ||
| using Task = System.Threading.Tasks.Task; | ||
| using IAsyncServiceProvider = Microsoft.VisualStudio.Shell.IAsyncServiceProvider; | ||
| using VsServiceProvider = Microsoft.VisualStudio.OLE.Interop.IServiceProvider; | ||
|
|
||
| namespace NuGet.VisualStudio | ||
|
|
@@ -22,17 +23,12 @@ namespace NuGet.VisualStudio | |
| // REVIEW: Make this internal | ||
| public static class ServiceLocator | ||
| { | ||
| public static void InitializePackageServiceProvider(IServiceProvider provider) | ||
| public static void InitializePackageServiceProvider(IAsyncServiceProvider provider) | ||
| { | ||
| if (provider == null) | ||
| { | ||
| throw new ArgumentNullException(nameof(provider)); | ||
| } | ||
|
|
||
| PackageServiceProvider = provider; | ||
| PackageServiceProvider = provider ?? throw new ArgumentNullException(nameof(provider)); | ||
| } | ||
|
|
||
| public static IServiceProvider PackageServiceProvider { get; private set; } | ||
| public static IAsyncServiceProvider PackageServiceProvider { get; private set; } | ||
|
|
||
| public static TService GetInstanceSafe<TService>() where TService : class | ||
| { | ||
|
|
@@ -96,23 +92,33 @@ public static async Task<TInterface> GetGlobalServiceAsync<TService, TInterface> | |
| // and so this method can RPC into main thread. Switch to main thread explictly, since method has STA requirement | ||
| await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); | ||
|
|
||
| return await GetGlobalServiceFreeThreadedAsync<TService, TInterface>(); | ||
| if (PackageServiceProvider != null) | ||
| { | ||
| var result = await PackageServiceProvider.GetServiceAsync(typeof(TService)); | ||
| var service = result as TInterface; | ||
| if (service != null) | ||
| { | ||
| return service; | ||
| } | ||
| } | ||
|
|
||
| return Package.GetGlobalService(typeof(TService)) as TInterface; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we add telemetry for when this or this is called?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What would be trying to learn? Maybe something else?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll merge this now, so it can runt through the nightly validations, but i'll be hpapy to discuss further improvements to this. |
||
| } | ||
|
|
||
| public static Task<TInterface> GetGlobalServiceFreeThreadedAsync<TService, TInterface>() where TInterface : class | ||
| public static async Task<TInterface> GetGlobalServiceFreeThreadedAsync<TService, TInterface>() where TInterface : class | ||
| { | ||
| if (PackageServiceProvider != null) | ||
| { | ||
| var result = PackageServiceProvider.GetService(typeof(TService)); | ||
| var result = await PackageServiceProvider.GetServiceAsync(typeof(TService)); | ||
| var service = result as TInterface; | ||
|
|
||
| if (service != null) | ||
| { | ||
| return Task.FromResult(service); | ||
| return service; | ||
| } | ||
| } | ||
|
|
||
| return Task.FromResult(Package.GetGlobalService(typeof(TService)) as TInterface); | ||
| return Package.GetGlobalService(typeof(TService)) as TInterface; | ||
| } | ||
|
|
||
| private static async Task<TService> GetDTEServiceAsync<TService>() where TService : class | ||
|
|
@@ -123,7 +129,7 @@ private static async Task<TService> GetDTEServiceAsync<TService>() where TServic | |
|
|
||
| private static async Task<TService> GetComponentModelServiceAsync<TService>() where TService : class | ||
| { | ||
| IComponentModel componentModel = await GetGlobalServiceAsync<SComponentModel, IComponentModel>(); | ||
| IComponentModel componentModel = await GetGlobalServiceFreeThreadedAsync<SComponentModel, IComponentModel>(); | ||
| return componentModel?.GetService<TService>(); | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.