Manipulating Failed Workflow Instances

Often, when we work with Windows Workflow Foundation (WF), we may encounter scenarios where our workflows may end up in an erroneous suspended state. This usually happens during development ... and sometimes in production due to missing validations (oops!), low coverage unit testing and lack of proper exception handling.

In WF3.5, we can easily Abort or Terminate failed workflows with the WorkflowInstance class. However, after a quick run through with my developer, Sylvester, we have discovered that the WorkflowInstance class has now been marked as obsolete in WF4.5 (actually since WF4). It has now been superseded with classes in the System.Activities namespace.

Searching for alternatives, we manage to discover the following method to achieve similar results.

// Define workflow instance store.
SqlWorkflowInstanceStore instanceStore =
    new SqlWorkflowInstanceStore(
        ConfigurationManager.ConnectionStrings["WorkflowStore"].ConnectionString);

// Setup the workflow with a definition.
WorkflowApplication app = new WorkflowApplication(new MyWorkflow());

// Assign the instance store.
app.InstanceStore = instanceStore;

// Load the workflow instance.
app.Load(new Guid("[Put Your Instance's GUID Here]"));

// Restart the workflow execution.
app.Run();

// Terminate the workflow instance.
app.Terminate("Terminated due to unrecoverable errors.");


You will need to add a reference to the System.Activities.DurableInstancing namespace.

Take note that when an error occurs, our workflow will be in a suspended state. Therefore, we will need to first call Run to resume the workflow and then call Terminate. If you have configured instanceCompletionAction="DeleteAll" in the sqlWorkflowInstanceStore setting in the config file, you will not be able to view the terminate reason in your WorkflowInstanceStore database, since all completed workflow instances will be deleted.

No comments:

Post a Comment

Popular Post