在等待post异步方法的结果时,任务被取消了内部异常。(聚合异常)

时间:2016-02-16 07:34:10

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

  

当我尝试在一个查询下执行两个查询时,我收到此错误   postasync调用。我已经为每个执行查询使用了parallel。

请在下面找到图片:enter image description here

这是执行异步方法的代码:

  Parallel.ForEach(commands, c =>
            {
                using (ConnectionManager connectionMetaData = new ConnectionManager())
                {
                connectionMetaData.Open(this.settings.ConnectionString);
                AdomdCommand command = new AdomdCommand(c.Value, connectionMetaData.Connection);
                command.CommandTimeout = timeOut;
                result.Add(c.Key, Cube.ConvertCellSetToDataTable(command.ExecuteCellSet()));
                command.Dispose();
                Thread.Sleep(150);
                }
            });

API调用代码:

 using (HttpClient client = new HttpClient(handler))
        {
            client.BaseAddress = new Uri(BaseAddress);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.Timeout.Add(new TimeSpan(1, 0, 0));
            MediaTypeFormatter jsonFormatter = new JsonMediaTypeFormatter();
            HttpContent content = new ObjectContent<dynamic>(Content, jsonFormatter);
            try
            {
                HttpResponseMessage response = client.PostAsync(ConfigurationManager.AppSettings[Report + "RequestURI"] + Action, content).Result;
                if (response.IsSuccessStatusCode)
                {
                    var data = response.Content.ReadAsStringAsync().Result;
                    return JsonConvert.DeserializeObject<T>(data);
                }
                else
                    throw new Exception();
            }
            catch (AggregateException ex)
            {
                throw ex;
            }
        }

0 个答案:

没有答案