繁体   English   中英

WebApi和异步方法

[英]WebApi and async methods

我非常迷失于.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时它们将不会完成(这实际上是在使void异步时发生的情况,但我发现这是大不了)。 如何使return routes在调用之前等待所有异步方法完成?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM