Parallel.ForEach并行下载文件时遇ThreadAbortException(超时超5分钟)的最佳解决方法问询
Parallel.ForEach并行下载文件时遇ThreadAbortException(超时超5分钟)的最佳解决方法问询
我现在在做一个.NET 4.8的应用,用Parallel.ForEach循环并行从Web API下载多个文件,但最近碰到个棘手的问题:时不时会抛出ThreadAbortException,错误提示是「The Maximum Wait/Sleep/Join time has been exceeded (00:05:00)」。我找了半天,都没找到怎么延长Parallel.ForEach里线程的最大等待超时时间。
下面是出问题的示例代码:
// create the httpclient using (HttpClient httpClient = new HttpClient()) { // now try to get documents in a parallel threaded task. Parallel.ForEach(documentIds, new ParallelOptions { MaxDegreeOfParallelism = Convert.ToInt32(Math.Ceiling((Environment.ProcessorCount * 0.50) * 2.0))}, (documentId) => { try { // post the data to the service using (HttpResponseMessage response = httpClient.PostAsync(serviceURL, new StringContent( requestBody.ToString())).GetAwaiter().GetResult()) { // show error if service failed if (response.IsSuccessStatusCode == false) { throw new Exception("Error getting content. " + response?.ReasonPhrase); } // get back content using (Stream responseStream = response.Content.ReadAsStreamAsync() .GetAwaiter().GetResult()) { // write the file to the FileSystem } } } catch (Exception ex) { // eat the error // NOTE: This does catch the ThreadAbortException but it breaks out // of the Parallel.ForEach BUT I want it to continue with other documents } }); }
更新1:临时解决但不符合最佳实践
我后来勉强让程序跑起来了,但这个方案明显有问题——根源在HttpClient上。如果我把HttpClient的创建移到Parallel.ForEach循环内部,每次迭代都新建一个实例,确实能捕获ThreadAbortException而且不会中断整个循环。但微软的最佳实践明确说了不能频繁创建多个HttpClient,不然会导致大量套接字在被回收前一直处于打开状态,时间长了肯定出问题。
现在我想找个既符合最佳实践,又能解决超时中断问题的办法。
备注:内容来源于stack exchange,提问作者goroth
相关产品推荐
相关产品推荐

