Je rencontre quelques problèmes où j'appelle flatten
sur un AggregateException
, mais à l'intérieur, il en reste UN AUTRE AggregateException
! Cela signifie évidemment qu'ils se propagent dans la chaîne et sont transformés en une autre variable AggregateException
. Existe-t-il un moyen d'aplatir récursivement TOUTES les exceptions internes agrégées? Habituellement, j'utilise le délégué handle pour les traiter, mais il renvoie false s'il existe une autre AggregateExceeption interne. Est-ce que je ne les gère pas correctement?
EDIT: Étant donné que j'appelle déjà Flatten, il semble que le problème, c’est que le problème n’est pas détecté plus tard. Voici le code où j'appelle Flatten (). Pour être utilisée dans la trace de pile, cette méthode s'appelle WriteExceptionRecord (string, FileInfo):
do
{
try
{
using (var stream = file.Open(FileMode.Append, FileAccess.Write, FileShare.None))
{
using (StreamWriter writer = new StreamWriter(stream))
{
await writer.WriteLineAsync(data);
}
}
}
catch (AggregateException ex)
{
ex.Flatten().Handle((x) =>
{
if (x is IOException)
{
retryNeeded = true;
retryLeft--;
Thread.Sleep(500);
return true;
}
logger.ErrorException("Could not write to exception file: " + data, ex);
return false;
});
}
}
while (retryNeeded && retryLeft > 0);
Cependant, la trace de la pile montre qu'elle n'est pas capturée ici. Au lieu de cela, il est pris bien plus tard dans la pile d'appels. Ci-dessous, la trace avec certaines informations d'identification supprimées pour des raisons de sécurité:
System.AggregateException: One or more errors occurred. --->
System.AggregateException: One or more errors occurred. --->
System.IO.IOException: The process cannot access the file 'X:\Production\ProductionBatches\DataEntry\J\PD\Exception.csv' because it is being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
at System.IO.FileInfo.Open(FileMode mode, FileAccess access, FileShare share)
at PDI.LoadFileProcessing.<WriteExceptionRecord>d__21.MoveNext() in c:\Users\XYZ\Development\PDI\PDI\LoadFileProcessing.cs:line 328
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at PDI.LoadFileProcessing.<ExceptionRecordProcessing>d__17.MoveNext() in c:\Users\XYZ\Development\PDI\PDI\LoadFileProcessing.cs:line 316
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at PDI.ProcessPipeline.<>c__DisplayClass9.<<ProcessBatch>b__2>d__13.MoveNext() in c:\Users\XYZ\Development\PDI\PDI\ProcessPipeline.cs:line 61
--- End of inner exception stack trace ---
--- End of inner exception stack trace ---
---> (Inner Exception #0) System.AggregateException: One or more errors occurred. ---> System.IO.IOException: The process cannot access the file 'X:\Production\ProductionBatches\DataEntry\J\PD\Exception.csv' because it is being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
at System.IO.FileInfo.Open(FileMode mode, FileAccess access, FileShare share)
at PeopleDocImporter.LoadFileProcessing.<WriteExceptionRecord>d__21.MoveNext() in c:\Users\XYZ\Development\PDI\PDI\LoadFileProcessing.cs:line 328
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at PDI.LoadFileProcessing.<ExceptionRecordProcessing>d__17.MoveNext() in c:\Users\XYZ\Development\PDI\PDI\LoadFileProcessing.cs:line 316
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at PDI.ProcessPipeline.<>c__DisplayClass9.<<ProcessBatch>b__2>d__13.MoveNext() in c:\Users\XYZ\Development\PDI\PDI\ProcessPipeline.cs:line 61
--- End of inner exception stack trace ---
---> (Inner Exception #0) System.IO.IOException: The process cannot access the file 'X:\Production\ProductionBatches\DataEntry\J\PD\Exception.csv' because it is being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
at System.IO.FileInfo.Open(FileMode mode, FileAccess access, FileShare share)
at PDI.LoadFileProcessing.<WriteExceptionRecord>d__21.MoveNext() in c:\Users\XYZ\Development\PDI\PDI\LoadFileProcessing.cs:line 328
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at PDI.LoadFileProcessing.<ExceptionRecordProcessing>d__17.MoveNext() in c:\Users\XYZ\Development\PDI\PDI\LoadFileProcessing.cs:line 316
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at PDI.ProcessPipeline.<>c__DisplayClass9.<<ProcessBatch>b__2>d__13.MoveNext() in c:\Users\XYZ\Development\PDI\PDI\ProcessPipeline.cs:line 61<---
<---
System.AggregateException: One or more errors occurred. ---> System.AggregateException: One or more errors occurred. ---> System.IO.IOException: The process cannot access the file 'X:\Production\ProductionBatches\DataEntry\J\PD\Exception.csv' because it is being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
at System.IO.FileInfo.Open(FileMode mode, FileAccess access, FileShare share)
at PDI.LoadFileProcessing.<WriteExceptionRecord>d__21.MoveNext() in c:\Users\XYZ\Development\PDI\PDI\LoadFileProcessing.cs:line 328
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at PDI.LoadFileProcessing.<ExceptionRecordProcessing>d__17.MoveNext() in c:\Users\XYZ\Development\PDI\PDI\LoadFileProcessing.cs:line 316
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at PDI.ProcessPipeline.<>c__DisplayClass9.<<ProcessBatch>b__2>d__13.MoveNext() in c:\Users\XYZ\Development\PDI\PDI\ProcessPipeline.cs:line 61
--- End of inner exception stack trace ---
---> (Inner Exception #0) System.AggregateException: One or more errors occurred. ---> System.IO.IOException: The process cannot access the file 'X:\J\PD\Exception.csv' because it is being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
at System.IO.FileInfo.Open(FileMode mode, FileAccess access, FileShare share)
at PDI.LoadFileProcessing.<WriteExceptionRecord>d__21.MoveNext() in c:\Users\XYZ\Development\PDI\PDI\LoadFileProcessing.cs:line 328
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at PDI.LoadFileProcessing.<ExceptionRecordProcessing>d__17.MoveNext() in c:\Users\XYZ\Development\PDI\PDI\LoadFileProcessing.cs:line 316
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at PDI.ProcessPipeline.<>c__DisplayClass9.<<ProcessBatch>b__2>d__13.MoveNext() in c:\Users\XYZ\Development\PDI\PDI\ProcessPipeline.cs:line 61
--- End of inner exception stack trace ---
---> (Inner Exception #0) System.IO.IOException: The process cannot access the file 'X:\Production\ProductionBatches\DataEntry\J\PD\Exception.csv' because it is being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
at System.IO.FileInfo.Open(FileMode mode, FileAccess access, FileShare share)
at PDI.LoadFileProcessing.<WriteExceptionRecord>d__21.MoveNext() in c:\Users\XYZ\Development\PDI\PDI\LoadFileProcessing.cs:line 328
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at PDI.LoadFileProcessing.<ExceptionRecordProcessing>d__17.MoveNext() in c:\Users\XYZ\Development\PDI\PDI\LoadFileProcessing.cs:line 316
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at PDI.ProcessPipeline.<>c__DisplayClass9.<<ProcessBatch>b__2>d__13.MoveNext() in c:\Users\XYZ\Development\PDI\PDI\ProcessPipeline.cs:line 61<---
À propos: Ceci est appelé par les blocs TPL-Dataflow.
Oui, il y a exactement ce que vous demandez:
AggreggateException.Flatten()
va passer et tout compresser en une seule exception AggregateException. Vous pouvez donc l'utiliser pour parcourir toutes les exceptions internes comme ceci:
try
{
// something dangerous
}
catch (AggregateException ae)
{
foreach(var innerException in ae.Flatten().InnerExceptions)
{
// handle error
}
}
Lien MSDN: http://msdn.Microsoft.com/en-us/library/system.aggregateexception.flatten.aspx
Gardez à l'esprit que la méthode 'flatten' vous donnera une liste d'exceptions, mais peut toujours vous laisser avec une InnerExceptions aplatie à l'intérieur de chaque exception.
Alors j'ai trouvé que ce n'était pas vraiment suffisant:
try
{
// something dangerous
}
catch (AggregateException ae)
{
foreach(Exception innerException in ae.Flatten().InnerExceptions)
{
Console.WriteLine(innerException.Message());
}
}
Parce que cette exception:
System.Net.Http.HttpRequestException: une erreur s'est produite lors de l'envoi de la demande. ---> System.Net.WebException: impossible de se connecter au serveur distant ---> System.Net.Sockets.SocketException: une tentative de connexion a échoué car la partie connectée n'a pas répondu correctement après un certain temps ou une connexion établie a échoué car l'hôte connecté n'a pas répondu 192.168.42.55:443 sur System.Net.Sockets.Socket.EndConnect (IAsyncResult asyncResult) sur System.Net.ServicePoint.ConnectSocketInternal (connectivité booléenne, Socket s4, Socket s6, Socket & socket, adresse IP et adresse, état ConnectSocketState, IAsyncResult asyncResult, Exception et exception) --- Fin de trace de pile d'exception interne --- sur System.Net.HttpWebRequest.EndGetRequestStream (IAsyncResult asyncResult, TransportContext & context) at System.Net.Http.HttpClientHandler.GetRequestStreamCallback (IAsyncResult ar) --- Fin de trace de pile d'exception interne ---
Finirait comme ça:
Une erreur s'est produite lors de l'envoi de la demande.
Le correctif ressemblait à ceci:
foreach(Exception exInnerException in aggEx.Flatten().InnerExceptions)
{
Exception exNestedInnerException = exInnerException;
do
{
if (!string.IsNullOrEmpty(exNestedInnerException.Message))
{
Console.WriteLine(exNestedInnerException.Message);
}
exNestedInnerException = exNestedInnerException.InnerException;
}
while (exNestedInnerException != null);
}
Résultant en:
Une erreur s'est produite lors de l'envoi de la demande.
Impossible de se connecter au serveur distant
Une tentative de connexion a échoué car la partie connectée n'a pas répondu correctement après un certain temps ou la connexion établie a échoué car l'hôte connecté n'a pas répondu. 192.168.42.54:443
J'espère que ça aide quelqu'un.
C'est une vieille question, mais le problème rencontré par l'OP est que wait n'expose pas l'exception AggregateException de la tâche attendue, mais simplement la première exception dans AggregateException. Le bloc catch (AggregateException ex) est donc ignoré et l'exception est interceptée plus loin dans la pile. Donc, le code aurait dû être 'simplement':
retryNeeded = false;
do
{
try
{
if (retryNeeded)
await Task.Delay(500); // substituted for Thread.Sleep
using (var stream = file.Open(FileMode.Append, FileAccess.Write, FileShare.None))
{
using (StreamWriter writer = new StreamWriter(stream))
{
await writer.WriteLineAsync(data);
retryNeeded = false;
}
}
}
catch (IOException)
{
retryNeeded = true;
retryLeft--;
}
catch (Exception ex)
{
logger.ErrorException("Could not write to exception file: " + data, ex);
throw;
}
} while (retryNeeded && retryLeft > 0);
return (retryLeft > 0);
Alternativement, La méthode d'extension WithAllExceptions de Jon Skeet permet de "protéger" le comportement de l'AggregateException en encapsulant la tâche dans une autre tâche afin d'obtenir une exception AggregateException contenant une AggregateException et d'attendre le renvoi de l'original/intérieure AggregateException.
NOTE: AggregateException.Flatten 'aplatit' de manière récursive, comme le montre l'exemple de la page MSDN .
EDIT: Retard amélioré lors de la relance pour éviter de définir un mauvais exemple asynchrone.
Normalement AggregateException est utilisé pour consolider plusieurs échecs en un seul objet exception jetable.
try {
Task.WaitAll(tasks)
}
catch (AggregateException ae) {
ae.Handle((x) =>
{
if (x is UnauthorizedAccessException) // This we know how to handle.
{
//do your code here
}
return true; //if you do something like this all exceptions are marked as handled
});
}
Essayez l'exemple de code ci-dessous, cela devrait éviter await
de dérouler la AggregateException
et de lancer la première exception AggregateException au point où task.result est appelé.
var task = writer.WriteLineAsync(data);
await task.ContinueWith(t => { }, TaskContinuationOptions.ExecuteSynchronously));
return task.Result;