Skip to main content

Workflow Foundation 4 - Part 2 - Executing workflows


In my introductory post I had focused on giving a brief overview of workflow foundation-4 highlighting what is new and how things have changed from 3.5 to 4. You can check this out here. This post will be a very simple one in which I am going to focus on how to execute workflows. Executing workflows is very simple in WF4.

There are basically two approaches -

  1. Use WorkflowInvoker
  2. Use WorkflowApplication

Using WorkflowInvoker is the simplest of the two approaches. Some of the disadvantages of this method are -

  • You cannot persist the workflow instance
  • You cannot unload or resume bookmarks

You can execute the workflow synchronously or asynchronously both using WorkflowInvoker. To execute the workflow synchronously using the WorkflowInvoker you simply call its static method Invoke and pass the workflow object to it.

Here is a simple example -


Sequence seqWorkflow = new Sequence {
Activities = { new WriteLine {Text = "Hello"}, new WriteLine {Text = "World using WorkflowInvoker.Invoke"} }
};
WorkflowInvoker.Invoke(seqWorkflow);


To execute the workflow asynchronously using the WorkflowInvoker you instantiate the WorkflowInvoker and call its instance method BeginInvoke and EndInvoke. You will need to define a callback method to pass to BeginInoke.

Here is a simple example -


static void Main(string[] args){
WorkflowInvoker wflInvoker = new WorkflowInvoker(seqWorkflow);
String stateInfo = "Asynchronous Execution";
IAsyncResult result = wfInvoker.BeginInvoke(new AsyncCallback(WorkflowCompleted), stateInfo);
//calling EndInvoke here will block untill the workflow completes.
//we can also call EndInvoke from callback.
wfInvoker.EndInvoke(result);
}
static void WorkflowCompleted(IAsyncResult result) {
Console.Writeline("Completed!!!");
}


Using WorkflowApplication provides a richer model for executing workflows. Some of the advantages of using WorkflowApplication are -

  • Notification of life-cycle events.
  • Resume bookmarks
  • Persist a workflow instance
  • Reload a previously persisted workflow instance

To execute workflow using WorkflowApplication we need to instantiate it, subscribe to its life cycle events and then call its Run method.

Here is a simple example -



static void Main(string[] args) {
Sequence seqWorkflow = new Sequence {
Activities = { new WriteLine {Text = "Hello"}, new WriteLine {Text = "World using WorkflowInvoker.Invoke"} }
};
WorkflowApplication wkflwApp = new WorkflowApplication(seqWorkflow);
wkflwApp.Aborted = new Action(AbortedWorkflow);
wkflwApp.Completed = new Action(CompletedWorkflow);
wkflwApp.Run();

};
static void AbortedWorkflow(WorkflowApplicationAbortedEventArgs e) {
Console.WriteLine("aborted");
}
static void CompletedWorkflow(WorkflowApplicationCompletedEventArgs e) {
Console.WriteLine("completed");
}

There are following life cycle events that you can subscribe to -

  • Aborted
  • Completed
  • Idle
  • PersistableIdle
  • Unload
  • OnUnhandledException

That is all there is to know about executing workflows in WF4. There is however one particular topic which I purposefully left out and that is passing in parameters / arguments to a workflow and similarly extracting outputs / results from a workflow. That will be a separate topic in itself and will be covered later as dive further deep into WF4.



Comments

Popular posts from this blog

Health Framework - Apple vs Android

In the past few years we have seen mobile and its apps rise and shine transforming many industries in its wake. However, the growth in health and fitness category has been less spectacular at 49% compared to overall mobile app industry which grew at 115% in the year 2013 (source Flurry Analytics). A few years ago Microsoft and Google attempted to make inroads into the health and fitness sector by bringing web based products to store and maintain health and fitness information like MS HealthVault and Google Health with not so spectacular results. Next came innovations by Fitbit in the wearables sector for activity tracking, in 2011 and 2012 they introduced first wireless activity trackers to sync using Bluetooth. This was followed by the entry of Jawbone into health sector with its announcements of Up wristband and accompanying app. These have had better success resulting in many startups joining the wearables product bandwagon.  Late to the stage almos...

Notes on Castle MonoRail

  Sometime back I was doing a small POC on Castle MonoRail. So here are my quick notes on this. MonoRail is an MVC Framework from Castle inspired by ActionPack. MonoRail enforces separation of concerns with Controller handling application flow, models representing data and View taking care of the presentation logic. To work with MonoRail you need Castle Assemblies. It also utilizes nHibernate You can use Castle MonoRail Project Wizard or create the project manually. Project structure – Content Css Images Controllers HomeController.cs Models Views Home \ index.vm Layouts \ Default.vm ...

Quick notes on Git

  I have been away from writing anything for a long time and instead have been fooling around with other stuffs like just plain reading, growing mustache, trying to learn swimming, trying to learn to play acoustic guitar and trying my hands at photography. To be honest I have not given up on them yet but neither have I been able to hang on to them in a disciplined manner. So here I am back to my writing after a long gap. This time its going to be quick notes on Git . Git is a file repository. As opposed to other repositories like SVN Git thinks of its data more like a snapshot of a mini filesystem. All operations in Git are local. (Entire history of the project is stored locally in your working directory) Browsing project history. Viewing all changes to a file. Git uses Checksum to track repository items Everything in Git is check-summed It uses SHA-1 hash for generating check sum values All a...