Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 921f8c5

Browse files
committed
Add generic GetServiceAsync to IGitHubServiceProvider
Keep it consistent with GetService<T> and GetService<T, Ret>. Fix failing VSGitExt unit tests.
1 parent f23fd24 commit 921f8c5

File tree

4 files changed

+14
-7
lines changed

4 files changed

+14
-7
lines changed

src/GitHub.Exports/Services/IGitHubServiceProvider.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Threading.Tasks;
3+
using System.Diagnostics.CodeAnalysis;
34
using System.ComponentModel.Composition.Hosting;
45
using System.Runtime.InteropServices;
56
using GitHub.VisualStudio;
@@ -30,6 +31,9 @@ Ret GetService<T, Ret>() where T : class
3031
/// or if it's null, the service will be removed without checking for ownership</param>
3132
void RemoveService(Type t, object owner);
3233

33-
Task<object> GetServiceAsync(Type serviceType);
34+
Task<T> GetServiceAsync<T>() where T : class;
35+
36+
[SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter")]
37+
Task<Ret> GetServiceAsync<T, Ret>() where T : class where Ret : class;
3438
}
3539
}

src/GitHub.TeamFoundation.14/Services/VSGitExt.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ async Task ContextChangedAsync()
9696

9797
async Task<bool> TryInitialize()
9898
{
99-
gitService = (IGitExt)await serviceProvider.GetServiceAsync(typeof(IGitExt));
99+
gitService = await serviceProvider.GetServiceAsync<IGitExt>();
100100
if (gitService != null)
101101
{
102102
gitService.PropertyChanged += (s, e) =>

src/GitHub.VisualStudio/Services/GitHubServiceProvider.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ public IServiceProvider GitServiceProvider
7272

7373
public T TryGetService<T>() where T : class => theRealProvider.TryGetService<T>();
7474

75-
public Task<object> GetServiceAsync(Type serviceType) => theRealProvider.GetServiceAsync(serviceType);
75+
public Task<T> GetServiceAsync<T>() where T : class => theRealProvider.GetServiceAsync<T>();
76+
77+
public Task<Ret> GetServiceAsync<T, Ret>() where T : class where Ret : class => theRealProvider.GetServiceAsync<T, Ret>();
7678
}
7779

7880
/// <summary>
@@ -312,6 +314,7 @@ public void Dispose()
312314
GC.SuppressFinalize(this);
313315
}
314316

315-
public Task<object> GetServiceAsync(Type serviceType) => asyncServiceProvider.GetServiceAsync(serviceType);
317+
public async Task<T> GetServiceAsync<T>() where T : class => (T)await asyncServiceProvider.GetServiceAsync(typeof(T));
318+
public async Task<Ret> GetServiceAsync<T, Ret>() where T : class where Ret : class => (Ret)await asyncServiceProvider.GetServiceAsync(typeof(T));
316319
}
317320
}

test/UnitTests/GitHub.TeamFoundation/VSGitExtTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public void GetServiceIGitExt_WhenSccProviderContextIsActive(bool isActive, int
2525

2626
var target = CreateVSGitExt(context, sp: sp);
2727

28-
sp.Received(expectCalls).GetService<IGitExt>();
28+
sp.Received(expectCalls).GetServiceAsync<IGitExt>();
2929
}
3030

3131
[TestCase(true, 1)]
@@ -39,7 +39,7 @@ public void GetServiceIGitExt_WhenUIContextChanged(bool activated, int expectCal
3939
var eventArgs = new VSUIContextChangedEventArgs(activated);
4040
context.UIContextChanged += Raise.Event<EventHandler<VSUIContextChangedEventArgs>>(context, eventArgs);
4141

42-
sp.Received(expectCalls).GetService<IGitExt>();
42+
sp.Received(expectCalls).GetServiceAsync<IGitExt>();
4343
}
4444

4545
[Test]
@@ -218,7 +218,7 @@ static VSGitExt CreateVSGitExt(IVSUIContext context = null, IGitExt gitExt = nul
218218
var contextGuid = new Guid(Guids.GitSccProviderId);
219219
factory.GetUIContext(contextGuid).Returns(context);
220220
sp.GetService<IVSUIContextFactory>().Returns(factory);
221-
sp.GetService<IGitExt>().Returns(gitExt);
221+
sp.GetServiceAsync<IGitExt>().Returns(gitExt);
222222
var vsGitExt = new VSGitExt(sp, factory, repoFactory);
223223
vsGitExt.PendingTasks.Wait();
224224
return vsGitExt;

0 commit comments

Comments
 (0)