gpt4 book ai didi

c# - 聚合异常 "One or more errors occurred.An error occurred while sending the request."

转载 作者:太空宇宙 更新时间:2023-11-03 20:54:53 31 4
gpt4 key购买 nike

我试图从我的 WCF .Net Framework 4.5 向 API rest 发布一个文件。这是我的代码:

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

}

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

有谁能帮我解决这个问题吗?提前谢谢你

最佳答案

聚合异常总是可以被解包以发现真正的原因。尝试在这样的 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
}
}

关于c# - 聚合异常 "One or more errors occurred.An error occurred while sending the request.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51533760/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com