我正在使用任務並行庫來設置任務鏈,如下所示,但我得到了一個奇怪的異常處理經驗,我不明白。AggregateException我發現沒有異常我期望
我使用Parallel.ForEach並調用一個包含對以下方法的調用的Action。這個Parallel.ForEach被封裝在一個try ... catch(AggregateException)中,當發生異常時 - 就像它在一個並行分支中那樣 - 一個SchemaValidation異常,那麼我期望在AggregateException中看到這種異常。
但是,我得到的是'任務被取消' - TaskCanceledException。我的SchemaValidationException何去何從?
private static void ProcessChunk(Task<ISelectedChunk> selectionTask,
IRepository repository,
IIdentifiedExtractChunk identifiedExtractChunk,
IBatchRunConfiguration batchRunConfiguration,
IBatchRun batchRun,
ILog log,
IAuthenticationCertificate authenticationCertificate,
IFileSystem fileSystem,
long batchRunRid)
{
var transformationTask = selectionTask.ContinueWith(TransformationFunction.Transformation(identifiedExtractChunk, batchRunConfiguration, batchRun),
TaskContinuationOptions.NotOnFaulted);
var schemaValidationTask = transformationTask.ContinueWith(SchemaValidationFunction.SchemaValidationTask(batchRunConfiguration),
TaskContinuationOptions.NotOnFaulted);
var compressTask = schemaValidationTask.ContinueWith(CompressFunction.CompressTask(identifiedExtractChunk),
TaskContinuationOptions.NotOnFaulted);
var encryptTask = compressTask.ContinueWith(EncryptionFunction.EncryptTask(authenticationCertificate),
TaskContinuationOptions.NotOnFaulted);
var fileGenerationTask = encryptTask.ContinueWith(FileGenerationFunction.FileGenerationTask(identifiedExtractChunk, batchRunConfiguration, fileSystem),
TaskContinuationOptions.NotOnFaulted);
// Take the time before we start the processing
DateTime startBatchItemProcessing = DateTime.Now;
// Start with the Selection Task
selectionTask.Start();
// And wait on the last task in the chain
fileGenerationTask.Wait();
// Take the time at the end of the processing
DateTime endBatchItemProcessing = DateTime.Now;
// Record all the relevant information and add it to the collection
IBatchChunkProcessed batchChunkProcessed = GetBatchItemProcessed(identifiedExtractChunk, batchRunRid, fileGenerationTask.Result, transformationTask.Result.Item2, startBatchItemProcessing, endBatchItemProcessing);
BatchItemsProcessed.Add(batchChunkProcessed);
爲什麼你甚至在這裏使用'任務'?爲什麼不按順序執行所有的方法? – svick 2012-04-04 12:44:59
該代碼似乎與該問題沒有任何關係。發佈捕獲和處理異常的代碼。 – 2012-04-04 13:15:27
@svick - 傳入的選擇任務是並行的。此外,這些是離散的積木任務,可以在其他情況下輕鬆地重新使用 - 並且可能成爲根本任務。 – Ciaran 2012-04-04 15:49:39