The new Windows Workflow Foundation 4 runtime

With WF3 there was a central workflow runtime environment called the WorkflowRuntime and used to manage the lifetime of workflow instances. In WF4 this central class no longer exists and we manage individual workflows.

To compare the two, this is what a minimal console application looks like in WF3. It basically runs a workflow and prints a message when done:

static void Main(string[] args)


{


    using (WorkflowRuntime workflowRuntime = new WorkflowRuntime())


    {


        workflowRuntime.WorkflowCompleted += delegate(object sender, WorkflowCompletedEventArgs e)


        {


            Console.WriteLine("The workflow has completed.");


        };


 


        WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(WorkflowConsoleApplication1.Workflow1));


        instance.Start();


 


        Console.WriteLine("Waiting for the workflow to complete.");


        Console.ReadLine();


    }


}

 

The same functionality in WF4 looks very different:

static void Main(string[] args)


{


    WorkflowInstance myInstance = new WorkflowInstance(new Sequence1());


 


    myInstance.OnCompleted += delegate(WorkflowCompletedEventArgs e)


    {


        Console.WriteLine("The workflow has completed.");


    };


    myInstance.Run();


 


    Console.WriteLine("Waiting for the workflow to complete.");


    Console.ReadLine();


}

 

The most important thing to note is that there is no longer a WorkflowRuntime object, instead we create a new WorkflowInstance object passing in a workflow instance (compared to a workflow type before).

 

There are some benefits as well as drawbacks to not having a WorkflowRuntime.

The main benefit is that it makes creating and running workflows much easier. No longer do we need to keep track of a singleton and use that to do everything. Another benefit is we can register event handler per workflow instance, makes it easier to do different things for different types of workflows. In fact the OnCompleted isn’t an event but a delegate so you can add only a single handler.

The drawback is that general configuration. Like recording workflow errors, tracking execution or workflow persistence must be done for each instance while before we could just do it on the workflow runtime.

Some things haven’t changed. In both WF3 and WF4 running the workflow is done asynchronously so we need to wait, in both cases done using a Console.ReadLine().

 

Synchronous execution

With WF3 it was hard to have a workflow execute synchronously. Basically we had to add a different scheduler to the workflow runtime and explicitly tell the scheduler to execute the workflow. Of course this would affect every workflow created through the same WorkflowRuntime.

With WF4 there is a much easier model using the WorkflowInvoker class. All we need to do is call WorkflowInvoker.Invoke() passing in the workflow object and the workflow will execute synchronously on the same thread. Not only very useful for running the odd workflow but also for unit testing as any WorkflowElement, the base class for all activities, can be executed this way.

static void Main(string[] args)


{


    WorkflowInvoker.Invoke(new Sequence1());


 


    Console.WriteLine("The workflow has completed.");


    Console.ReadLine();


}



Nice right!



 



Enjoy.



[f1]
[f2]

One thought on “The new Windows Workflow Foundation 4 runtime

  1. Thank you for posting these – I’ve been looking for a good resource on WF4, and it looks like you’re doing great so far. =) Keep it coming!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>