我正在使用.NET库访问Visual Studio Team Services,并试图解决微软方面的一个明显的设计缺陷。显然,每个服务器/帐户不能有多个集合,所以我必须使用多个帐户,在这个例子中我将其称为集合,因为微软甚至已经明确表示they map to the same thing。我如何获得所有团队服务帐户中的所有工作项目?
我实际上想要实现的目标是列出所有我所属集合中的所有工作项目。我有一个使用GetAllCollections()获取我的所有集合的QueryWorkItems()方法。这种方法已经过测试,它的工作原理,它返回我有两个帐户。触发整个事物的顶层方法是AssignedWorkItems()。我的代码如下:
public static List<TfsTeamProjectCollection> GetAllCollections()
{
// Get collections/accounts associated with user
string request = "https://app.vssps.visualstudio.com/_apis/Accounts?memberId=" + versionControl.AuthorizedIdentity.TeamFoundationId + "&api-version=3.2-preview";
string content = MakeRequestToAPI(request).Result;
dynamic results = JsonConvert.DeserializeObject<dynamic>(content);
List<TfsTeamProjectCollection> collections = new List<TfsTeamProjectCollection>();
// Iterate through all collections
Parallel.ForEach((IEnumerable<dynamic>)results.value, collection =>
{
TfsTeamProjectCollection col = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri((string)collection.accountUri));
collections.Add(col);
});
return collections;
}
public static List<WorkItem> QueryWorkItems(string query)
{
List<WorkItem> workItems = new List<WorkItem>();
List<TfsTeamProjectCollection> collections = GetAllCollections();
//Parallel.ForEach(collections, collection =>
foreach(var collection in collections)
{
WorkItemCollection items = collection.GetService<WorkItemStore>().Query(query);
// Add each work item to the overall list
Parallel.For(0, items.Count, i =>
{
Console.WriteLine(items[i].Title);
lock (workItems)
{
workItems.Add(items[i]);
}
});
}
return workItems;
}
public static List<WorkItem> AssignedWorkItems()
{
Init(); //initializes variables like projectName, workItemStore and VersionControlServer(versionControl)
string myItems = "select * from issue where [System.AssignedTo][email protected]";
return QueryWorkItems(myItems);
}
当我打电话的AssignedWorkItems方法我得到一个登录提示,即使我有一个默认的连接已经建立:
我输入我的凭据后,虽然,在这条线:
WorkItemCollection items = collection.GetService().Query(query);
我得到以下错误:
An unhandled exception of type 'Microsoft.TeamFoundation.TeamFoundationServiceUnavailableException' occurred in Microsoft.TeamFoundation.Client.dll
Additional information: TF31002: Unable to connect to this Team Foundation Server: https://xxxxxx.vssps.visualstudio.com/ .
Team Foundation Server Url: https://xxxxxx.vssps.visualstudio.com/
Possible reasons for failure include:
The name, port number, or protocol for the Team Foundation Server is incorrect.
The Team Foundation Server is offline.
The password has expired or is incorrect.
有趣的是我每次运行这个错误中提到的URL在我有两个集合之间来回切换。任何想法为什么会发生这种情况?
谢谢,这似乎是这个问题。但我真的需要设置凭据吗?如果我没有登录,它会向我显示登录提示,如果我只是给了我想要的结果。 如果我确实需要设置凭据,我不能拥有相同的所有集合的凭据集?毕竟,我是他们中的一员。 – sonicadv27
这不是必需的,它只会节省您在代码执行时不输入凭据。您也可以使用'TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(“https://account.visualstudio.com”))'。 –