如何在TFS 2015对象模型:GetBuildsAsync中使用continuationtoken?

时间:2018-06-20 15:29:18

标签: tfs2015

我正在使用以下代码

    BuildHttpClient service = new BuildHttpClient(tfsCollectionUri, 
new Microsoft.VisualStudio.Services.Common.VssCredentials(true));

    var asyncResult = service.GetBuildsAsync(project: tfsTeamProject);
    var queryResult = asyncResult.Result;

这仅返回前199个版本。

似乎需要使用continuationtoken,但不确定如何执行此操作。文档说REST API将返回令牌。我正在使用对象模型,并且正在寻找如何检索令牌!

我正在使用Microsoft.TeamFoundationServer.Client v 14.102.0; Microsoft.TeamFoundationServer.ExtendedClient v 14.102.0,Microsoft.VisualStudio.Service.Client v 14.102.0和Microsoft.VisualStudio.Services.InteractiveClient v 14.102.0

问题 在使用TFS对象模型时,如何使用延续令牌**?

1 个答案:

答案 0 :(得分:0)

在第一次调用API之后,continuationToken位于响应标头中:

x-ms-continuationtoken: xxxx

无法从.net客户端库中检索它。您必须使用rest api检索标头​​信息。这是一个供您参考的示例:

using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace GetBuilds
{
    class Program
    {
        public static void Main()
        {
            Task t = GetBuilds();
            Task.WaitAll(new Task[] { t });
        }
        private static async Task GetBuilds()
        {
            try
            {
                var username = "xxxxx";
                var password = "******";

                using (var client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Accept.Add(
                        new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                        Convert.ToBase64String(
                            System.Text.ASCIIEncoding.ASCII.GetBytes(
                                string.Format("{0}:{1}", username, password))));

                    using (HttpResponseMessage response = client.GetAsync(
                                "http://tfs2015:8080/tfs/DefaultCollection/teamproject/_apis/build/builds?api-version=2.2").Result)
                    {
                        response.EnsureSuccessStatusCode();
                        string responseBody = await response.Content.ReadAsStringAsync();
                        Console.WriteLine(responseBody);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}