C# WebApi和异步方法

C# WebApi和异步方法,c#,.net,asynchronous,asp.net-web-api,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 HtmlAgilityPac

我对.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;
        }
私有静态列表parseRouteHTML(字符串结果,Cookie)
{
列表路由=新列表();
HtmlDocument HtmlDocument=新的HtmlAgilityPack.HtmlDocument();
htmlDocument.LoadHtml(结果);
int contNumber=1;
while(true)
{
HtmlNode divNode=htmlDocument.GetElementbyId(“cont”+contNumber);
if(divNode!=null)
{
HtmlNode table=divNode。选择SingleNode(“表”);
如果(表!=null)
{
字符串fullRoute=“”;
HtmlNodeCollection dataRows=table.SelectNodes(“tr[@align]”);
RouteDTO currentRoutes=新RouteDTO();
foreach(dataRows中的var dataRow)
{
currentRoutes.routes.Add(ParseRoutedAtarRow(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];
字符串pricelef=priceLink.Attributes[“href”].Value;
ParcePrice(currentRoutes、priceHref、cookie);
添加(当前路由);
}
contNumber++;
}
其他的
{
打破
}
}
返回路线;
}
私有静态void ParcePrice(路由到currentRoutes、字符串priceHref、Cookie Cookie)
{
var cookieContainer=新的cookieContainer();
var handler=new-HttpClientHandler(){CookieContainer=CookieContainer};
var httpClient=新的httpClient(处理程序);
cookieContainer.Add(cookie);
var priceMessage=httpClient.GetByteArrayAsync(“http://razpisanie.bdz.bg“+priceHref).结果;
var priceResponse=Encoding.UTF8.GetString(priceMessage,0,priceMessage.Length-1);
var priceInfo=ParsePriceResponse(priceResponse);
currentRoutes.priceInfo=价格信息;
}
私有静态void解析映射(路由到currentRoutes,HtmlNode mapLink)
{
var httpClient=新的httpClient();
字符串mapref=mapLink.Attributes[“href”].Value;
var mapMessage=httpClient.GetByteArrayAsync(“http://razpisanie.bdz.bg“+mapref).结果;
var mapResponse=Encoding.UTF8.GetString(mapMessage,0,mapMessage.Length-1);
string mapString=ParseMapResponse(mapResponse);
currentRoutes.imageBase64=映射字符串;
}

请不要介意糟糕的HTML解析(网站本身很糟糕),但是看看ParseMap和ParsePrice方法。它们发出web请求,因此需要异步。问题是,如果我使它们异步,我担心调用
返回路由
时它们将无法完成(这实际上是我使void异步时发生的情况,但我发现这是一个很大的禁忌)。如何使
返回路由
等待所有异步方法完成后再调用它?

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

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