2013-05-13 30 views
7

Tôi đang cố gọi phương thức PostAsync bằng System.Net.Http.HttpClient từ API Web. Tôi nhận được lỗi sau:Lỗi PostAsync HttpClient với Web Api - System.AggregateException "Một tác vụ đã bị hủy."

System.AggregateException "A task was canceled."

Nhiệm vụ:

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

Code:

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(); 
      }); 
    } 

Tôi giả sử responseTask sẽ buộc phương pháp này để chạy đồng bộ?

Đây là ứng dụng WPF chứ không phải ASP.NET.

Trả lời

3

Xét về gỡ lỗi bạn có thể thử viết một phương pháp mở rộng để có được ngoại lệ :

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

Không đồng bộ, nhiệm vụ thứ hai cũng sẽ được thực hiện không đồng bộ nhưng bị xích với nhiệm vụ đầu tiên, do đó chỉ sau khi thực hiện tác vụ đầu tiên.

Dường như là nhiệm vụ đầu tiên - PostAsync được thực hiện với lỗi. Cố gắng nắm bắt TPL ngoại lệ tổng hợp và tìm thêm chi tiết trong bộ sưu tập ngoại lệ bên trong từ AggregateException Ví dụ như here hoặc đăng ký TaskScheduler.UnobservedTaskException và đăng nhập có tất cả các trường hợp ngoại lệ của bạn

10

Tôi đã nhận được lỗi này và theo dõi nó xuống HttpClient của tôi đã hết thời gian chờ. Thời gian chờ mặc định là 100 giây. Tôi thêm vào sau đây để tạo ra HttpClient.

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

Các vấn đề liên quan