AggregateException“发生一个或多个错误。发送请求时发生错误。”

时间:2018-07-26 07:56:54

标签: c# wcf httpclient

我尝试将文件从WCF .Net framework 4.5发布到API其余部分。这是我的代码:

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

    }

它不起作用并引发异常:“发生一个或多个错误。发送请求时发生错误。”

有人可以帮助我解决这个问题吗?预先谢谢你

1 个答案:

答案 0 :(得分:2)

总异常可以始终被解开以发现真正的原因。尝试在try-catch中编写客户呼叫,如下所示:

    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 
     }   
   }