我有一堆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;
}发布于 2010-03-24 00:21:21
是的,一旦您了解了WF4活动是如何工作的,这一点都不难。
我可以写一个很长的故事,但因为Richard Blewett已经在博客上写过一个重试活动,它可以执行你所描述的重试操作,所以我只打算链接到那篇博文here。唯一缺少的是持久化,但这应该很容易添加。
只需创建AttemptServiceCall活动并将其作为主体添加即可。鉴于这听起来像是一个潜在的长时间运行的操作,我建议使用AsyncCodeActivity作为基类。
https://stackoverflow.com/questions/2501447
复制相似问题