7

मैं वेब एपीआई से System.Net.Http.HttpClient का उपयोग कर PostAsync विधि को कॉल करने का प्रयास कर रहा हूं। मैं निम्नलिखित त्रुटि मिलती है:वेब एपीआई के साथ PostAsync HttpClient त्रुटि - System.AggregateException "एक कार्य रद्द कर दिया गया था।"

System.AggregateException "A task was canceled."

टास्क:

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

कोड:

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

मुझे लगता है responseTask तुल्यकालिक चलाने के लिए विधि के लिए बाध्य करेगा?

यह एक WPF एप्लिकेशन है, एएसपी.NET नहीं।

उत्तर

3

लोग इन डिबगिंग अगर आप एक्सटेंशन विधि लेखन की कोशिश कर सकते अपवाद प्राप्त करने के संदर्भ में :

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

तुल्यकालिक रूप से नहीं, दूसरा कार्य एसिंक भी निष्पादित किया जाएगा लेकिन पहले कार्य के साथ जंजीर होगा, इसलिए केवल पहले कार्य निष्पादित किए जाने के बाद ही।

पहला कार्य होने लगता है - PostAsync को त्रुटि के साथ निष्पादित किया गया था। here तरह AggregateException से TPL एकत्रित अपवादों को पकड़ने और भीतरी अपवाद संग्रह में और अधिक जानकारी प्राप्त करने के लिए उदाहरण के लिए प्रयास करें या TaskScheduler.UnobservedTaskException की सदस्यता और वहाँ अपने सभी अपवाद

10

मुझे यह वही त्रुटि मिल रही थी और इसे मेरे एचटीपी क्लाइंट पर ट्रैक किया गया था। डिफ़ॉल्ट टाइमआउट 100 सेकंड है। मैंने निम्नलिखित एचटीपी क्लाइंट के निर्माण में जोड़ा।

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

संबंधित मुद्दे