WF 4.0: How to Execute a Worklflow (WorkflowInvoker vs WorkflowInstance)

Note: This post is based on Visual Studio 2010 Beta 1 which is the latest version available in the time of writing this post, so by the time this technology ships, there are probably things that will be slight different.

Execute Worklflow (WorkflowInvoker WorkflowInstanceThere are two ways to execute a workflow:

  • Using the WorkflowInstance class
  • Using the WorkflowInvoker class

Execute Workflows using WorkflowInstace

When you create a new Sequential Workflow Console Application, Visual Studio creates the basic code needed to execute the workflow inside the Main method:

static void Main(string[] args)

{

  AutoResetEvent syncEvent = new AutoResetEvent(false);

 

  WorkflowInstance myInstance = new WorkflowInstance(new Sequence1());

 

  myInstance.OnCompleted = delegate(WorkflowCompletedEventArgs e)

  {

    syncEvent.Set();

  };

 

  myInstance.OnUnhandledException = delegate(WorkflowUnhandledExceptionEventArgs e)

  {

    Console.WriteLine(e.UnhandledException.ToString());

    return UnhandledExceptionAction.Terminate;

  };

 

  myInstance.OnAborted = delegate(WorkflowAbortedEventArgs e)

  {

    Console.WriteLine(e.Reason);

    syncEvent.Set();

  };

 

  myInstance.Run();

 

  syncEvent.WaitOne();

 

}

1. A workflow instance is created and gets a reference to the root activity of the workflow. The WorkflowInstance class lets us control the running instance of the workflow and exposes several methods for this such as Run(), Abort(), Cancel(), Load(), Unload etc.

2. Since the workflow execution is scheduled to another thread, we need to block the main thread until the workflow is completed or terminated. To do this, we:

  • Create an AutoResetEvent initialized to false,
  • Set the event to true when the workflow is completed, and
  • In the main thread - wait for the workflow to complete before exiting the method.

3. In order to handle errors in the workflow such as exceptions or termination, in addition to handling the OnCompleted event, we are also handling the OnHandledException and OnAborted events. Note that when the workflow has an unhandled exception, the program can still decide how it wants to end the workflow execution, whether it is by aborting, cancelling or terminating the workflow. If the workflow is terminated, the OnCompleted event will also be raised.

Execute Workflows using WorkflowInvoker

WorkflowInvoker lets you invoke a workflow much more easily than the previous approach. It takes the instance of the workflow or activity to run and executes it synchronously.

WorkflowInvoker.Invoke(new Sequence1());

There are additional overloads of this method that takes parameters and a TimeSpan in which the workflow should complete within. I’ll talk in more details about passing parameters to workflow in a subsequent post, but in general this is all we have to know about WorkflowInvoker.

Choosing between WorkflowInstance and WorkflowInvoker

If you have a simple workflow that should run synchronously – use WorkflowInvoker. This is useful also in when unit testing workflows (more on this in a later post).

If you have a long running workflow that you want to control – persist, abort, cancel etc – use WorkflowInstance.

Enjoy!