/// <summary> /// Helper method to use async/await with IAsyncServiceProvider implementation /// </summary> /// <param name="asyncServiceProvider">IAsyncServciceProvider instance</param> /// <param name="serviceType">Type of the Visual Studio service requested</param> /// <returns>Service object as type of T</returns> public static async Task <T> GetServiceAsync <T>(this IAsyncServiceProvider asyncServiceProvider, Type serviceType) where T : class { T returnValue = null; await ThreadHelper.JoinableTaskFactory.RunAsync(async() => { var serviceTypeGuid = serviceType.GUID; var serviceInstance = await asyncServiceProvider.QueryServiceAsync(ref serviceTypeGuid); // We have to make sure we are on main UI thread before trying to cast as underlying implementation // can be an STA COM object and doing a cast would require calling QueryInterface/AddRef marshaling // to main thread via COM. await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); returnValue = serviceInstance as T; }); return(returnValue); }
private async Task <IVsContainedLanguageFactory> GetContainedLanguageFactoryAsync() { Guid languageServiceId = await GetLanguageServiceId(); if (languageServiceId == Guid.Empty) { return(null); } IOleAsyncServiceProvider serviceProvider = await _serviceProvider.GetValueAsync(); object service = await serviceProvider.QueryServiceAsync(ref languageServiceId); // NOTE: While this type is implemented in Roslyn, we force the cast on // the UI thread because they are free to change this to an STA object // which would result in an RPC call from a background thread. await _projectVsServices.ThreadingService.SwitchToUIThread(); return((IVsContainedLanguageFactory)service); }