2013-05-13 27 views
7

Próbuję wywołać metodę PostAsync przy użyciu System.Net.Http.HttpClient z Web API. Pojawia się następujący błąd:Błąd PostAsync HttpClient z Web Api - System.AggregateException "Zadanie zostało anulowane."

System.AggregateException "A task was canceled."

Zadanie:

Id = 1, Status = System.Threading.Tasks.TaskStatus.Canceled, Method = "{null}", Result = "{Not yet computed}"

Kod:

using (HttpClientHandler handler = new HttpClientHandler()) 
{ 
    handler.Credentials = new NetworkCredential("MyUsername", "[email protected]"); 

    using (HttpClient client = new HttpClient(handler)) 
    { 
     var postData = new List<KeyValuePair<string, string>>(); 
     postData.Add(new KeyValuePair<string, string>("status", "Hello world")); 

     HttpContent content = new FormUrlEncodedContent(postData); 

     var responseTask = client.PostAsync(url, content).ContinueWith(
      (postTask) => 
      { 
       postTask.Result.EnsureSuccessStatusCode(); 
      }); 
    } 

zakładam responseTask zmusi sposób uruchomić synchronicznie?

Jest to aplikacja WPF, a nie ASP.NET.

Odpowiedz

3

Pod względem debugowania można spróbować napisać metodę rozszerzenia aby uzyskać wyjątek :

public static HttpResponseMessage PostAsyncSafe(this HttpClient client, string requestUri, string content) 
     { 
      var requestContent = new StringContent(content, Encoding.UTF8, "application/x-www-form-urlencoded"); 
      return PerformActionSafe(() => (client.PostAsync(requestUri, requestContent)).Result); 
     } 

public static HttpResponseMessage PerformActionSafe(Func<HttpResponseMessage> action) 
     { 
      try 
      { 
       return action(); 
      } 
      catch (AggregateException aex) 
      { 
       Exception firstException = null; 
       if (aex.InnerExceptions != null && aex.InnerExceptions.Any()) 
       { 
        firstException = aex.InnerExceptions.First(); 

        if (firstException.InnerException != null) 
         firstException = firstException.InnerException; 
       } 

       var response = new HttpResponseMessage(HttpStatusCode.InternalServerError) 
       { 
        Content = 
         new StringContent(firstException != null 
              ? firstException.ToString() 
              : "Encountered an AggreggateException without any inner exceptions") 
       }; 

       return response; 
      } 
     } 
0

Nie synchronicznie, drugie zadanie będzie również wykonywane asynchronicznie, ale powiązane z pierwszym zadaniem, dlatego tylko po wykonaniu pierwszego zadania.

Wydaje się być pierwszym zadaniem - PostAsync został wykonany z błędem. Spróbuj złapać OC zagregowane wyjątki i znaleźć więcej szczegółów w wewnętrznej kolekcji wyjątków od AggregateException Na przykład jak here lub subskrybować TaskScheduler.UnobservedTaskException i rejestrować tam wszystkie wyjątki

10

Otrzymałem ten sam błąd i śledziłem go do mojego HttpClient upłynął limit czasu. Domyślny limit czasu to 100 sekund. Dodałem następujące do utworzenia HttpClient.

HttpClient httpClient = new HttpClient(); 
httpClient.Timeout = TimeSpan.FromMinutes(10);