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 -
- Use WorkflowInvoker
- 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 {
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){
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) {
};
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