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

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 ...

Workflow Foundation 4 - Part 3 - Data storage and management

This is my third post on WF4. First one was an introductory post on WF4 and in second one we focused on executing workflows. In the this post I am going to focus on the topic of data storage and management. Every business process or flow depends on data. When you think of data there are three elements to it as listed below - Variables - for storing data Arguments - for passing data Expressions - for manipulating data. Let us first look at the variables. Variables are storage locations for data. Variables are declared before using them just like in any other languages like C# or VB.Net. Variables are defined with a specific scope. When you create a variable in an activity the scope of the variable becomes that activity's scope. Variables can also have access modifiers like None, Mapped or ReadOnly. Let us look at an example where we will create two variables and assign a scope to them along with access modifiers. //Declare a sequence activitiy Sequence seqWf = new Sequence(); //de...

Introduction to Workflow Foundation 4 (WF4)

I finally decided to pick-up my blogging once more. Since I have been trying to learn Windows Workflow Foundation 4 (WF4) I thought I might as well start off with this topic. WF4 is a development framework that enables you to create a workflow and embed it in a .Net application. It is neither an executable application nor a language. It provides a set of tools for declaring a workflow, activities to create your logic and to define control flow and a runtime for executing the resulting application definition. It also provides services for persistence of state, tracking and bookmarking. You can create your workflow directly in code, in mark-up or in a combination of both. Workflow provide us with two major advantages - Creating unified application logic. Making application logic scalable. Workflow Authoring styles - Sequential Workflow executes a set of contained activities in a sequential manner. Workflow Foundation was introduced in the .Net 3.0 and updated in 3.5. In .net 4 it has bee...