22using System . Windows ;
33using System . Threading ;
44using System . Threading . Tasks ;
5+ using System . ComponentModel . Design ;
56using System . ComponentModel . Composition ;
67using System . Runtime . InteropServices ;
78using GitHub . Api ;
1314using GitHub . Services . Vssdk . Commands ;
1415using GitHub . ViewModels . GitHubPane ;
1516using GitHub . VisualStudio . UI ;
16- using GitHub . Services . Vssdk ;
1717using Microsoft . VisualStudio ;
1818using Microsoft . VisualStudio . ComponentModelHost ;
1919using Microsoft . VisualStudio . Shell ;
@@ -29,15 +29,35 @@ namespace GitHub.VisualStudio
2929 [ ProvideMenuResource ( "Menus.ctmenu" , 1 ) ]
3030 [ ProvideAutoLoad ( Guids . UIContext_Git , PackageAutoLoadFlags . BackgroundLoad ) ]
3131 [ ProvideOptionPage ( typeof ( OptionsPage ) , "GitHub for Visual Studio" , "General" , 0 , 0 , supportsAutomation : true ) ]
32- public class GitHubPackage : AsyncMenuPackage
32+ public class GitHubPackage : AsyncPackage
3333 {
3434 static readonly ILogger log = LogManager . ForContext < GitHubPackage > ( ) ;
3535
36- protected async override Task InitializeMenusAsync ( OleMenuCommandService menuService )
36+ protected override async Task InitializeAsync ( CancellationToken cancellationToken , IProgress < ServiceProgressData > progress )
3737 {
3838 LogVersionInformation ( ) ;
39+ await base . InitializeAsync ( cancellationToken , progress ) ;
3940 await GetServiceAsync ( typeof ( IUsageTracker ) ) ;
41+ await InitializeMenus ( ) ;
42+ }
43+
44+ // The IDesignerHost and ISelectionService services are requested by MenuCommandService.EnsureVerbs().
45+ // When called from a non-Main thread this would throw despite the fact these services don't exist.
46+ // This override allows IMenuCommandService.AddCommands to be called form a background thread.
47+ protected override object GetService ( Type serviceType )
48+ => ( serviceType == typeof ( ISelectionService ) || serviceType == typeof ( IDesignerHost ) ) ? null : base . GetService ( serviceType ) ;
49+
50+ void LogVersionInformation ( )
51+ {
52+ var packageVersion = ApplicationInfo . GetPackageVersion ( this ) ;
53+ var hostVersionInfo = ApplicationInfo . GetHostVersionInfo ( ) ;
54+ log . Information ( "Initializing GitHub Extension v{PackageVersion} in {$FileDescription} ({$ProductVersion})" ,
55+ packageVersion , hostVersionInfo . FileDescription , hostVersionInfo . ProductVersion ) ;
56+ }
4057
58+ async Task InitializeMenus ( )
59+ {
60+ var menuService = ( IMenuCommandService ) ( await GetServiceAsync ( typeof ( IMenuCommandService ) ) ) ;
4161 var componentModel = ( IComponentModel ) ( await GetServiceAsync ( typeof ( SComponentModel ) ) ) ;
4262 var exports = componentModel . DefaultExportProvider ;
4363
@@ -52,14 +72,6 @@ protected async override Task InitializeMenusAsync(OleMenuCommandService menuSer
5272 exports . GetExportedValue < IShowGitHubPaneCommand > ( ) ) ;
5373 }
5474
55- void LogVersionInformation ( )
56- {
57- var packageVersion = ApplicationInfo . GetPackageVersion ( this ) ;
58- var hostVersionInfo = ApplicationInfo . GetHostVersionInfo ( ) ;
59- log . Information ( "Initializing GitHub Extension v{PackageVersion} in {$FileDescription} ({$ProductVersion})" ,
60- packageVersion , hostVersionInfo . FileDescription , hostVersionInfo . ProductVersion ) ;
61- }
62-
6375 async Task EnsurePackageLoaded ( Guid packageGuid )
6476 {
6577 var shell = await GetServiceAsync ( typeof ( SVsShell ) ) as IVsShell ;
0 commit comments