如何实现await关键字?

时间:2017-03-28 03:03:05

标签: api asynchronous tfs

我编写了一个代码,使用tfs web api从TFS 2013服务器获取项目。由于方法执行的时间太长,例如1.5分钟,所以我想把这个方法视为异步。所以我在返回类型中添加了异步和任务。当我在visual studio中看到时,会显示一条消息,表明methord没有等待关键字。我应该在哪里添加await关键字。我是异步编程的新手。

 public override async Task<List<Project>> GetProjects()
        {

        List<Project> retunResult = new List<Project>();

        using (var http = new HttpClient(GetHttpHandler()))
            {
            var response = http.GetAsync(_baseUrl + "_apis/projectCollections?" + tfsWebApiVersionSring).Result;
            if (response.IsSuccessStatusCode)
                {
                response.EnsureSuccessStatusCode();
                TFS2013TeamProjectCollection.Rootobject obj = JsonConvert.DeserializeObject<TFS2013TeamProjectCollection.Rootobject>(response.Content.ReadAsStringAsync().Result);
                if (obj != null)
                    {
                    foreach (TFS2013TeamProjectCollection.Value projColl in obj.value)
                        {

                        if (projColl.state == "Started")
                            {
                            var responseProj = http.GetAsync(_baseUrl + projColl.name + "/_apis/projects?" + tfsWebApiVersionSring).Result;
                            if (responseProj.IsSuccessStatusCode)
                                {
                                responseProj.EnsureSuccessStatusCode();
                                TFS2013TeamProject.Rootobject obj1 = JsonConvert.DeserializeObject<TFS2013TeamProject.Rootobject>(responseProj.Content.ReadAsStringAsync().Result);
                                if (obj1 != null)
                                    {
                                    Project p;
                                    foreach (TFS2013TeamProject.Value TeamProj in obj1.value)
                                        {
                                        p = new Project();
                                        p.collectionName = TeamProj.collection.name;
                                        p.description = TeamProj.description;
                                        p.id = TeamProj.id;
                                        p.name = TeamProj.name;
                                        p.collectionName = TeamProj.collection.name;
                                        p.url = TeamProj.url;

                                        retunResult.Add(p);
                                        }
                                    }
                                }
                            }
                        }
                    return retunResult;
                    }
                }
            }
        return null;
        }

2 个答案:

答案 0 :(得分:0)

你根本不应该使用Result。您的代码中的每个位置:

var resp = http.GetAsync(url).Result;

你应该:

var resp = await http.GetAsync(url); 

答案 1 :(得分:0)

您可以参考此代码段,了解如何使用TFS REST API以异步方式获取内容。

public static async void RESTAPIMethod()
    {
        HttpClientHandler authtHandler = new HttpClientHandler()
        {
            Credentials = CredentialCache.DefaultNetworkCredentials
        };

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

            using (HttpResponseMessage response = client.GetAsync(
                "Put the REST API URL here").Result)
            {
                response.EnsureSuccessStatusCode();
                var responseBody = await response.Content.ReadAsStringAsync();

            }
        }
    }

关于使用TFS REST API的另一种方法:

您也可以为项目安装此Nuget package。然后在此包中使用这些程序集来运行带有C#的REST API。这与上述方法不同,它更方便。以下是在Async方法中使用 Microsoft.TeamFoundation.Build.WebApi 程序集获取构建信息的示例。 示例What object returns from TFS2015 RestAPI