【发布时间】:2020-01-07 05:41:05
【问题描述】:
我们正在使用 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();
}
}
这两种方法有什么区别,哪种方法是推荐的方法并且适用于 TFS 2017 版本?
任何帮助或建议或链接都会很棒。
OAuth 身份验证是否也适用于 TFS,就像它适用于 Azure Devops 一样?
【问题讨论】:
标签: c# tfs azure-devops-rest-api tfs-sdk