AggregateException "One or more errors occurred.An error occurred while sending the request."

34,893

An aggregate exception can always be unwrapped to discover the real cause. Try to write your client call inside a try-catch like this:

    try {


    //Some risky client call that will call parallell code / async /TPL or in some way cause an AggregateException 

   }
   catch (AggregateException err){
    foreach (var errInner in err.InnerExceptions) {
     Debug.WriteLine(errInner); //this will call ToString() on the inner execption and get you message, stacktrace and you could perhaps drill down further into the inner exception of it if necessary 
     }   
   }
Share:
34,893
Viet Nguyen
Author by

Viet Nguyen

Updated on July 31, 2020

Comments

  • Viet Nguyen
    Viet Nguyen over 3 years

    I tried to post a file to an API rest from my WCF .Net framework 4.5. Here is my code:

    public string CreateConclusion(string[] instanceUIDs)
        {
            var root = @"C:\";
            string filename = "1.2.840.114257.1.9.1245.56421.52314.1119854.01248.dcm";
    
                using (var client = new HttpClient())
                {
                    var stream = new FileStream(root + filename, FileMode.Open);
    
                    using (var content =
                        new MultipartFormDataContent("Upload----" + DateTime.Now.ToString(CultureInfo.InvariantCulture)))
                    {
                        content.Add(new StreamContent(stream), "fileToUpload", filename);
    
                        using (var message = client.PostAsync("https://localhost:44343/api/ConclusionReports/UploadFile", content).Result)
                        {
                            var input = message.Content.ReadAsStringAsync();
    
                            return !string.IsNullOrWhiteSpace(input.Result) ? Regex.Match(input.Result, @"http://\w*\.directupload\.net/images/\d*/\w*\.[a-z]{3}").Value : null;
                        }
                    }
                }
    
        }
    

    It doesn't work and throw an exception: "One or more errors occurred.An error occurred while sending the request."

    Does anyone can help me to solve this problem? Thank you in advance

    • Anders Carstensen
      Anders Carstensen almost 6 years
      An AggregateException contains one or more exceptions in its InnerExceptions property. You should add those exceptions to your original post.
  • rkralston
    rkralston over 4 years
    err.ToString(); will also capture all the inner exceptions and stack tracing.
  • Tore Aurstad
    Tore Aurstad over 4 years
    Thanks, I was not aware of this. But the debug experience is perhaps better. You can also rethrow certain exception and log others using this approach.