Beispiel #1
0
        async Task <object> CreateService(IAsyncServiceContainer container, CancellationToken cancellationToken, Type serviceType)
        {
            if (serviceType == null)
            {
                return(null);
            }

            if (container != this)
            {
                return(null);
            }

            if (serviceType == typeof(IGitHubServiceProvider))
            {
                //var sp = await GetServiceAsync(typeof(SVsServiceProvider)) as IServiceProvider;
                var result = new GitHubServiceProvider(this, this);
                await result.Initialize();

                return(result);
            }
            else if (serviceType == typeof(ILoginManager))
            {
                // These services are got through MEF and we will take a performance hit if ILoginManager is requested during
                // InitializeAsync. TODO: We can probably make LoginManager a normal MEF component rather than a service.
                var serviceProvider = await GetServiceAsync(typeof(IGitHubServiceProvider)) as IGitHubServiceProvider;

                var keychain      = serviceProvider.GetService <IKeychain>();
                var oauthListener = serviceProvider.GetService <IOAuthCallbackListener>();

                // HACK: We need to make sure this is run on the main thread. We really
                // shouldn't be injecting a view model concern into LoginManager - this
                // needs to be refactored. See #1398.
#pragma warning disable VSTHRD011 // Use AsyncLazy<T>
                var lazy2Fa = new Lazy <ITwoFactorChallengeHandler>(() =>
                                                                    ThreadHelper.JoinableTaskFactory.Run(async() =>
                {
                    await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
                    return(serviceProvider.GetService <ITwoFactorChallengeHandler>());
                }));
#pragma warning restore VSTHRD011 // Use AsyncLazy<T>

                return(new LoginManager(
                           keychain,
                           lazy2Fa,
                           oauthListener,
                           ApiClientConfiguration.ClientId,
                           ApiClientConfiguration.ClientSecret,
                           ApiClientConfiguration.MinimumScopes,
                           ApiClientConfiguration.RequestedScopes,
                           ApiClientConfiguration.AuthorizationNote,
                           ApiClientConfiguration.MachineFingerprint));
            }
            else if (serviceType == typeof(IUsageService))
            {
                var sp = await GetServiceAsync(typeof(IGitHubServiceProvider)) as IGitHubServiceProvider;

                var environment = new Rothko.Environment();
                return(new UsageService(sp, environment));
            }
            else if (serviceType == typeof(IUsageTracker))
            {
                var usageService = await GetServiceAsync(typeof(IUsageService)) as IUsageService;

                var serviceProvider = await GetServiceAsync(typeof(IGitHubServiceProvider)) as IGitHubServiceProvider;

                var settings = await GetServiceAsync(typeof(IPackageSettings)) as IPackageSettings;

                return(new UsageTracker(serviceProvider, usageService, settings));
            }
            else if (serviceType == typeof(IVSGitExt))
            {
                var vsVersion = ApplicationInfo.GetHostVersionInfo().FileMajorPart;
                return(new VSGitExtFactory(vsVersion, this, GitService.GitServiceHelper).Create());
            }
            else if (serviceType == typeof(IGitHubToolWindowManager))
            {
                return(this);
            }
            else if (serviceType == typeof(IPackageSettings))
            {
                await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

                var sp = new ServiceProvider(Services.Dte as Microsoft.VisualStudio.OLE.Interop.IServiceProvider);
                return(new PackageSettings(sp));
            }
            else if (serviceType == typeof(ITippingService))
            {
                return(new TippingService(this));
            }
            // go the mef route
            else
            {
                var sp = await GetServiceAsync(typeof(IGitHubServiceProvider)) as IGitHubServiceProvider;

                return(sp.TryGetService(serviceType));
            }
        }