diff --git a/Source/TeamMate/Services/ResolverService.cs b/Source/TeamMate/Services/ResolverService.cs index 7de0138..29ab166 100644 --- a/Source/TeamMate/Services/ResolverService.cs +++ b/Source/TeamMate/Services/ResolverService.cs @@ -1,4 +1,5 @@ using Microsoft.VisualStudio.Services.Graph.Client; +using Microsoft.VisualStudio.Services.Users; using System; using System.Collections.Generic; using System.Collections.ObjectModel; @@ -10,13 +11,15 @@ namespace Microsoft.Tools.TeamMate.Services { public class ResolverService { - private List GraphUserCache { get; set; } + private Dictionary GraphUserCache { get; set; } - private List GraphGroupCache { get; set; } + private Dictionary GraphGroupCache { get; set; } private List Tasks = new List(); - private async Task> FetchUsersAsync( + private bool Cached = false; + + private async Task> FetchUsersAsync( GraphHttpClient graphClient) { if (GraphUserCache != null) @@ -24,7 +27,7 @@ private async Task> FetchUsersAsync( return GraphUserCache; } - List users = new List(); + var users = new Dictionary(); string continuationToken = null; do @@ -33,7 +36,10 @@ private async Task> FetchUsersAsync( continuationToken = data.ContinuationToken != null ? data.ContinuationToken.First() : null; foreach (var user in data.GraphUsers) { - users.Add(user); + if (user.MailAddress != null) + { + users[user.MailAddress] = user.Descriptor; + } } } while (continuationToken != null); @@ -43,7 +49,7 @@ private async Task> FetchUsersAsync( return users; } - private async Task> FetchGroupsAsync( + private async Task> FetchGroupsAsync( GraphHttpClient graphClient) { if (GraphGroupCache != null) @@ -51,7 +57,7 @@ private async Task> FetchGroupsAsync( return GraphGroupCache; } - List groups = new List(); + var groups = new Dictionary(); string continuationToken = null; do @@ -61,7 +67,10 @@ private async Task> FetchGroupsAsync( continuationToken = data.ContinuationToken != null ? data.ContinuationToken.First() : null; foreach (var group in data.GraphGroups) { - groups.Add(group); + if (group.MailAddress != null) + { + groups[group.MailAddress] = group.Descriptor; + } } } while (continuationToken != null); @@ -71,11 +80,19 @@ private async Task> FetchGroupsAsync( return groups; } - public void FetchDataSync( + private void FetchDataSyncIfNeeded( GraphHttpClient client) { - Tasks.Add(FetchUsersAsync(client)); - Tasks.Add(FetchGroupsAsync(client)); + lock (Tasks) + { + if (!Cached) + { + Tasks.Add(FetchUsersAsync(client)); + Tasks.Add(FetchGroupsAsync(client)); + + Cached = true; + } + } } public async Task Resolve( @@ -87,14 +104,15 @@ public void FetchDataSync( return null; } + this.FetchDataSyncIfNeeded(client); + await Task.Run(() => { foreach (var task in this.Tasks) { task.Wait(); } }); foreach (var user in GraphUserCache) { - if (user.MailAddress != null && - (user.MailAddress.Contains(value))) + if (user.Key.Contains(value)) { - var storageKey = client.GetStorageKeyAsync(user.Descriptor).Result; + var storageKey = client.GetStorageKeyAsync(user.Value).Result; return storageKey.Value; } @@ -102,17 +120,15 @@ public void FetchDataSync( foreach (var group in GraphGroupCache) { - if (group.MailAddress != null && - (group.MailAddress.Contains(value))) + if (group.Key.Contains(value)) { - var storageKey = client.GetStorageKeyAsync(group.Descriptor).Result; + var storageKey = client.GetStorageKeyAsync(group.Value).Result; return storageKey.Value; } } throw new ArgumentException("Could not resolve '" + value + "'. Try the full email for the person and/or group."); - } } } diff --git a/Source/TeamMate/Services/VstsConnectionService.cs b/Source/TeamMate/Services/VstsConnectionService.cs index cd90b7c..1bf7a97 100644 --- a/Source/TeamMate/Services/VstsConnectionService.cs +++ b/Source/TeamMate/Services/VstsConnectionService.cs @@ -190,9 +190,6 @@ private async Task DoConnectAsync(ProjectInfo projectInfo, Cance projectContext.WorkItemFieldsByName = fields.ToDictionary(f => f.ReferenceName, StringComparer.OrdinalIgnoreCase); projectContext.RequiredWorkItemFieldNames = GetWorkItemFieldsToPrefetch(projectContext.WorkItemFieldsByName); - this.ResolverService.FetchDataSync( - graphClient); - return projectContext; } catch (Exception e)