Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 0.99 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Pattern for implementing sync methods in terms of non-parallell Task (Translating/Unwrapping AggregateExceptions)
  2. CallAsync().Wait();
  3. }
  4. catch( AggregateException ae)
  5. {
  6.     throw ae.Flatten().First()
  7.        
  8. void Call( )
  9. {
  10.     try
  11.     {
  12.         CallAsync().Wait();
  13.     }
  14.     catch ( AggregateException ex )
  15.     {
  16.         var translated = ex.InnerException ?? ex.Flatten().InnerExceptions.First();
  17.         if ( translated == null )
  18.             throw;
  19.         throw translated;                 }
  20. }
  21.  
  22. Task CallAsync(){ ...
  23.        
  24. void Call()
  25. {
  26.     try
  27.     {
  28.         CallAsync().Wait();
  29.     }
  30.     catch (AggregateException ex)
  31.     {
  32.         throw new MyConsistentWrapperException("An exception occurred while executing my workflow. Check the inner exception for more details.", ex.Flatten().InnerExceptions.First());
  33.     }
  34. }
  35.        
  36. void Call()
  37. {
  38.     try
  39.     {
  40.         CallAsync().Wait();
  41.     }
  42.     catch (AggregateException ex)
  43.     {
  44.         ExceptionDispatchInfo.Capture(ex.Flatten().InnerExceptions.First()).Throw();
  45.     }
  46. }