我遇到了工作流程中未发现的异常问题。我有一个由 TryCatch 包围的自定义 AsyncCodeActivity;无论如何,异常没有被捕获,更糟糕的是,我托管工作流的 IIS 池有时会重新启动。看着这个问题/答案(尽管 TryCatch 活动,异常从工作流中逃脱),我认为问题在于我重新抛出异常的方式。这就是我通常编写异步代码活动的方式:
protected override IAsyncResult BeginExecute(AsyncCodeActivityContext context, AsyncCallback callback, object state)
{
Action<object> execute = s => this.Execute();
var task = new Task(execute, state, CancellationToken.None, TaskCreationOptions.PreferFairness);
task.ContinueWith(s => callback(s));
task.Start();
return task;
}
protected override void EndExecute(AsyncCodeActivityContext context, IAsyncResult result)
{
var task = result as Task;
Contract.Assert(task != null);
if (task.IsFaulted && task.Exception != null)
{
Contract.Assert(
task.Exception.InnerException != null,
"It is expected that the inner exception in a task is not null");
const string Message = "An exception was thrown while doing something";
Logger.ErrorException(Message, task.Exception.InnerException);
throw new WorkflowApplicationException(Message, task.Exception.InnerException);
}
}
private void Execute()
{
// Do something here
}
这是处理异步代码活动中异常的正确方法吗?如果是,我应该如何防止我的工作流中止(有时重新启动 IIS)?谢谢