DCSIMG
WF 4.0: How to Execute a Worklflow (WorkflowInvoker vs WorkflowInstance) - Guy Burstein's Blog

Guy Burstein's Blog

Developer Evangelist @ Microsoft

News

Guy Burstein The Bu

Disclaimer
Postings are provided 'As Is' with no warranties and confer no rights.

Guy Burstein LinkedIn Profile

TwitterCounter for @bursteg

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

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!

Comments

Shuki said:

Hi Guy, as always thanks for the information.

we developed a big project with WF-Rule Engine 3.5,

Do you have any documents or link for what was changed in the new version of the new WF-Rule engine(4.0) ?

Thanks,

Shuki

# May 19, 2009 12:56 PM

Guy Burstein said:

Hi

Unfortunatelly, the rule engine was taken out of WF 4.0. It may appear in a future update or on codeplex, but we cannot guarantee it.

I would wait for annoucement on this topic by the dev. team.

hope this helps,

Guy

# May 19, 2009 1:31 PM

Shockey said:

What's going to happen with windows workflow and dyanmics 4.0?

# June 11, 2009 11:22 PM

Vagif Abilov said:

Guy,

You said that rule engine won't be a part of WF 4.0. Does this mean that it will be an independent part of .NET FX? What's going to happen with PolicyActivities from WF 3.x? And btw we are currently using rule engine without WF (being inspired by your blog post about WF rule parser). Will this be compatible with .NET FX 4.0?

# June 14, 2009 5:50 PM

Shuki said:

It seems that Rule engine back to town(on WF4.0)....in the last MSDN Pulse (thanks to you Guy) i founded the WF 4 Migration Guidance very usefull with great document about WF4 Rules Guidance, so as i understand WF4.0 keep the great capability of the rule engine.

Thanks,

Shuki

www.microsoft.com/.../details.aspx

# July 6, 2009 9:59 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: