我们正在使用 TFS 2015,并将迁移到 TFS 2017 版本。
由于我们有很多集合(大约 17 个),有时我们必须手动检查所有集合中的值。为了缓解这个问题,我计划查询 TFS Rest API,以获取值。
我对如何使用 Rest API 有一些疑问,因为我们可以使用以下方法:
1.) 使用 Nuget 包,例如:
Microsoft.TeamFoundationServer.Client
Microsoft.VisualStudio.Services.Client
Microsoft.VisualStudio.Services.InteractiveClient
这需要以下代码:
//Prompt user for credential
VssConnection connection = new VssConnection(new Uri(vstsCollectionUrl), new VssBasicCredential(string.Empty, pat));
//create a wiql object and build our query
Wiql wiql = new Wiql()
{
Query = "Select [State], [Title] " +
"From WorkItems " +
"Where [Work Item Type] = 'Bug' " +
"And [System.TeamProject] = '" + project + "' " +
"And [System.State] <> 'Closed' " +
"Order By [State] Asc, [Changed Date] Desc"
};
//create http client and query for resutls
WorkItemTrackingHttpClient witClient = connection.GetClient<WorkItemTrackingHttpClient>();
Wiql query = new Wiql() { Query = "SELECT [Id], [Title], [State] FROM workitems WHERE [Work Item Type] = 'Bug' AND [Assigned To] = @Me" };
WorkItemQueryResult queryResults = witClient.QueryByWiqlAsync(query).Result;
//Display reults in console
if (queryResults == null || queryResults.WorkItems.Count() == 0)
{
Console.WriteLine("Query did not find any results");
}
2.) 另一种方法是使用带有 PAT 或备用凭证的 HttpClient 访问 Rest API,如下所示:
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(
new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(
"Basic", Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(
$"{username}:{password}")));
using (HttpResponseMessage response = client.GetAsync(
$"https://dev.azure.com/{account}/_apis/projects").Result)
{
response.EnsureSuccessStatusCode();
repsonseBody = await response.Content.ReadAsStringAsync();
}
}
这 2 种方法之间有什么区别?哪种方法是推荐的方法并且适用于 TFS 2017 版本?
任何帮助、建议或链接都会很棒。
OAuth 身份验证是否也可以与 TFS 一起使用,就像与 Azure Devops 一样使用吗?
最佳答案
nuget 包中的 *HttpClient
类是 REST API 的包装器。所以它们的级别更高,应该更容易使用。他们还为所有结果和参数对象定义类。由于它们针对相同的 API,因此受到同等支持。
Also does OAuth Authentication work with TFS, as it does with Azure Devops ?
The following guidance is intended for Azure DevOps Services users, since OAuth 2.0 is not supported on Team Foundation Server or Azure DevOps Server.
关于c# - TFS Rest API 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57786521/