使用TransformBlock捕获原始异常

vto*_*ola 2 .net task task-parallel-library async-await tpl-dataflow

我正在使用TPL Dataflow库中的TransformBlock,我已经意识到在转换期间抛出异常时,我在"接收"方法中得到一个通用异常,但没有提到原始异常.

在这段代码中:

Func<Int32, Task<String>> transformer = async i => { await Task.Yield(); throw new ArgumentException("whatever error"); };
TransformBlock<Int32, String> transform = new TransformBlock<int, string>(transformer);
transform.Post(1);

try
{
    var x = await transform.ReceiveAsync();
}
catch (Exception ex)
{
    // catch
}
Run Code Online (Sandbox Code Playgroud)

例外ex包含:

System.InvalidOperationException was caught
  HResult=-2146233079
  Message=The source completed without providing data to receive.
  Source=System.Threading.Tasks.Dataflow
  StackTrace:
       at System.Threading.Tasks.Dataflow.Internal.Common.InitializeStackTrace(Exception exception)
    --- 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`1.GetResult()
       at DoubleQueueTest.Program.<testwhatever>d__5.MoveNext() in c:\Users\vtortola\Documents\Visual Studio 2013\Projects\DoubleQueueTest\DoubleQueueTest\Program.cs:line 43
  InnerException: 
Run Code Online (Sandbox Code Playgroud)

没有提到原始异常类型或其消息.有没有办法强迫它抛出原来的?或者至少,将它用作内部例外?

Ste*_*ary 5

你看到了一个例外ReceiveAsync.这InvalidOperationException是预期的行为.

如果你想检测或响应阻塞故障,则awaitIDataflowBlock.Completion属性.