调用其他活动的工作流程4.0代码活动(持续,延迟等)

时间:2010-03-23 15:57:25

标签: .net .net-4.0 workflow-foundation workflow-foundation-4

我有一堆Workflow Foundation 4.0 RC代码活动,它们使用Web服务并与数据库交谈,我想在其中添加一些错误处理。

我真的希望能够尝试调用我的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;
}

1 个答案:

答案 0 :(得分:2)

是的,一旦你知道WF4的活动如何运作,这一点并不困难。

我可以输入一个长篇故事,但正如Richard Blewett已经在博客上发布了一个Retry活动,它会执行您描述的完全重试操作,我将链接到该博客文章here。唯一缺少的是持久性,但应该很容易添加。

只需创建您的AttemptServiceCall活动并将其添加为正文。鉴于它听起来像是一个可能长时间运行的动作,我建议使用AsyncCodeActivity作为基类。