Workflow Foundation: The Hello World App

Posted on 8/1/2006 @ 6:37 AM in #Vanilla .NET by | Feedback | 8944 views

Okay so I just talked about the very very basics of what Workflow foundation is all about. That was all theory. Giving all theory to a programmer is like feeding grass to a lion. So let us change the mode to code, and write our first workflow application. And what better way to start, than a HelloWorld application?

So first, fire up Visual Studio (assuming that you have already installed the WF extensions for VS2005), and create a new project of type “Sequential Workflow Console Application” (as shown below).

Great, now go ahead and right click on the project, and "Add new Activity". Name the activity HelloWorld.cs.

Go to the code view of the activity, and override the Execute method of the activity to write a Console.WriteLine("Hello World!"). This is shown as below.

public partial class HelloWorld: SequenceActivity
      public HelloWorld()

    protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        Console.WriteLine("Hello World");
        return base.Execute(executionContext);


Now, stare deeply at the ActivityExecutionContext (AEC). Go on .. stare .. don't be shy.

Done staring?

Okay!! This is the heart of your workflow, I will be discussing this in much further detail in subsequent blogposts, but for now let us stay on track.

Now, you probably have a Workflow1.cs in your project. Double click on it, and open it. Drag Drop the "HelloWorld" activity from the toolbox to the surface of the workflow as shown below.

Go ahead and run it. You would notice that you can run this workflow, and you can also debug it in it's visual representation using the familiar F10/F11 keys. :). (And if you don't quit playing with that, you need some serious F1).

Now go ahead and add a second workflow, this time a Sequential Workflow with code separation. Just as before, go ahead and drag drop "HelloWorld" into this sequential workflow. you would note that this sequential workflow is represented in a .xoml file. If you were to open this workflow in notepad, it would look a bit like this.

<SequentialWorkflowActivity x:Class="WorkflowConsoleApplication1.Workflow2" x:Name="Workflow2" xmlns:ns0="clr-namespace:WorkflowConsoleApplication1" xmlns:x="" xmlns="">
      <ns0:HelloWorld x:Name="helloWorld1" />


This is the equivalent of the following C# code.

private HelloWorld helloWorld1;
void InitializeComponent()
    this.helloWorld1 = new WorkflowConsoleApplication1.HelloWorld();
    this.helloWorld1.Name = "helloWorld1";


And if you open the Workflow1.Designer.cs, you will see code that is very very similar to the above :). Thus the .cs workflow, and the .xoml workflow are essentially the same.

The next obvious question is, how do you run Workflow2 (xoml) not Workflow1 (cs).

Well, go ahead and open Program.cs. Here is the code for that:

using(WorkflowRuntime workflowRuntime = new WorkflowRuntime())
    AutoResetEvent waitHandle = new AutoResetEvent(false);
    workflowRuntime.WorkflowCompleted += delegate(object sender, WorkflowCompletedEventArgs e) {waitHandle.Set();};
    workflowRuntime.WorkflowTerminated += delegate(object sender, WorkflowTerminatedEventArgs e)
    WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(WorkflowConsoleApplication1.Workflow1));


Whoaa, observe carefully. You instantiated a WorkflowRuntime instance, and in there you did a workflowRuntime.CreateWorkflow. Then you simple started the WorkflowInstance.

THATS IT!! That is how your workflow runtime can run custom workflows.

Go ahead and run Workflow2 instead of Workflow1, and you will notice that both of them produce the same output - "Hello World".

Congratulations, you've just written your first workflow enabled application. In further blogposts, I will be diving deeper into different kinds of workflows, AEC, and various other facets of this new technology.


Sound off but keep it civil:

Older comments..