WebApi和异步方法

时间:2013-09-10 09:03:57

标签: c# .net asynchronous asp.net-web-api

我在.NET的新异步方法中相当迷失。我的问题是我有一个方法

private static List<RouteDTO> ParseRoutesHTML(string result, Cookie cookie)
        {
            List<RouteDTO> routes = new List<RouteDTO>();
            HtmlDocument htmlDocument = new HtmlAgilityPack.HtmlDocument();
            htmlDocument.LoadHtml(result);
            int contNumber = 1;
            while (true)
            {

                HtmlNode divNode = htmlDocument.GetElementbyId("cont"+contNumber);
                if (divNode != null)
                {
                    HtmlNode table = divNode.SelectSingleNode("table");
                    if (table != null)
                    {
                        string fullRoute = "";
                        HtmlNodeCollection dataRows = table.SelectNodes("tr[@align]");
                        RouteDTO currentRoutes = new RouteDTO();
                        foreach (var dataRow in dataRows)
                        {
                            currentRoutes.routes.Add(ParseRouteDataRow(dataRow));
                            //fullRoute+=ParseDataRow(dataRow)+" then ";
                        }
                        HtmlNodeCollection lastRow = table.SelectNodes("tr").Last().SelectSingleNode("td/div").SelectNodes("a");
                        HtmlNode mapLink = lastRow[1];

                        ParseMap(currentRoutes, mapLink);


                        HtmlNode priceLink = lastRow[2];
                        string priceHref = priceLink.Attributes["href"].Value;

                        ParcePrice(currentRoutes, priceHref, cookie);

                        routes.Add(currentRoutes);
                    }
                    contNumber++;
                }
                else
                {
                    break;
                }
            }
            return routes;
        }

private static void ParcePrice(RouteDTO currentRoutes, string priceHref, Cookie cookie)
        {
            var cookieContainer = new CookieContainer();
            var handler = new HttpClientHandler() { CookieContainer = cookieContainer };
            var httpClient = new HttpClient(handler);
            cookieContainer.Add(cookie);

            var priceMessage = httpClient.GetByteArrayAsync("http://razpisanie.bdz.bg" + priceHref).Result;
            var priceResponse = Encoding.UTF8.GetString(priceMessage, 0, priceMessage.Length - 1);
            var priceInfo = ParsePriceResponse(priceResponse);
            currentRoutes.priceInfo = priceInfo;
        }

        private static void ParseMap(RouteDTO currentRoutes, HtmlNode mapLink)
        {
            var httpClient = new HttpClient();
            string mapHref = mapLink.Attributes["href"].Value;
            var mapMessage = httpClient.GetByteArrayAsync("http://razpisanie.bdz.bg" + mapHref).Result;
            var mapResponse = Encoding.UTF8.GetString(mapMessage, 0, mapMessage.Length - 1);
            string mapString = ParseMapResponse(mapResponse);
            currentRoutes.imageBase64 = mapString;
        }

请不要介意糟糕的HTML解析(网站本身很糟糕),但请看一下ParseMap和ParsePrice方法。这些会产生Web请求,因此需要是异步的。问题是,如果我让它们异步,我担心在调用return routes时它们不会被完成(这实际上就是当我使虚拟异步时发生的事情,但我发现这是一个很大的禁忌)。如何让return routes等待所有异步方法在调用之前完成?

1 个答案:

答案 0 :(得分:1)

从每个解析方法返回Task,将它们放在列表或数组中,然后使用Task.WhenAll创建一个任务,该任务将在完成所有任务后完成并从您的操作中返回。