views:

419

answers:

1

I have a bunch of Workflow foundation 4.0 RC code activities that consume web services and talk to databases that I want to add some error handling in.

I would really like to be able to attempt to call my web service / db, catch any faults such as a communication failure and then retry the same operation in 1 hours time (after I have logged the exception).

Is there a way of doing something like this?

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;
}
+2  A: 

Yes that is not difficult at all once you know how WF4 activities work.

I could type a long story but as Richard Blewett has already blogged about a Retry activity that does the exact retry action you describe I am just going to link to that blog post here. The only thing missing is the persist but that should be easy to add.

Just create your AttemptServiceCall activity and add it as the body. Given that it sounds like a potentially long running action I would suggest using an AsyncCodeActivity as the base class.

Maurice
Thanks for your swift answer once again Maurice - you are the workflow king!
Lygpt