.net 工作流4.0编码活动调用其他活动(持续、延迟等) 我有一组工作流基础4 RC代码活动,它消耗Web服务并与数据库通信,我想在./p>中添加一些错误处理。

.net 工作流4.0编码活动调用其他活动(持续、延迟等) 我有一组工作流基础4 RC代码活动,它消耗Web服务并与数据库通信,我想在./p>中添加一些错误处理。,.net,.net-4.0,workflow-foundation,workflow-foundation-4,.net,.net 4.0,Workflow Foundation,Workflow Foundation 4,我真的希望能够尝试调用我的web服务/db,捕获任何故障,例如通信故障,然后在1小时内重试相同的操作(在我记录了异常之后) 有没有办法做到这一点 protected override void Execute(CodeActivityContext context) { Persist(); // I would like to invoke the persist activity like this if (!AttemptServiceCall()) { //

我真的希望能够尝试调用我的web服务/db,捕获任何故障,例如通信故障,然后在1小时内重试相同的操作(在我记录了异常之后)

有没有办法做到这一点

protected override void Execute(CodeActivityContext context) {

  Persist(); // I would like to invoke the persist activity like this

  if (!AttemptServiceCall()) {

        // I would like to invoke a delay activity like this
        Delay(new TimeSpan(0, 30, 0)); // wait 30 mins before trying again
        Execute(context); // call this activity again
  }
}

private bool AttemptServiceCall() {

  bool serviceCallSuccessful = true;

  try {
        myService.InvokeSomeMethod();
  }
  catch (CommunicationException ex) {
        myEventLogger.Log(ex);
        serviceCallSuccessful = false;
  }

  return serviceCallSuccessful;
}

是的,一旦你知道WF4活动是如何工作的,这一点都不难

我可以输入一个很长的故事,但是Richard Blewett已经在博客上写了一个重试活动,它执行了您描述的确切重试操作,因此我将链接到该博客文章。唯一缺少的是持久化,但这应该很容易添加


只需创建AttemptServiceCall活动并将其添加为主体。考虑到这听起来可能是一个长期运行的操作,我建议使用AsyncCodeActivity作为基类。

再次感谢您的快速回答Maurice-您是工作流之王!