This is the first in a number of articles that look at the new features in the WF 4.0, and as such the features might change or be missing in futures releases.
Windows Workflow Foundation 4.0 (WF), along with .NET 4.0 was announced at PDC 2008 and those present were lucky enough to get their hands on a VPC image. The Beta 1 VS2010 ISO is now available for download, you can get it from HERE.
So what's all the fuss about? Well put simply, WF as we know it has been given a complete overhaul from the ground up! They're rewritten the entire foundation and simplified it no end. WF is now far simpler to use and easier to extend. So in the coming series of articles I'll walk you through the new features showing how they differ to the 3.x version.
We're start off with the new Designers provided with WF 4.0. I can't show you the State Machine Designer, as it has been dropped from the 4.0 release. Instead we’ll be getting the Sequential Designer, and the new Flowchart Designer which is kind of a Sequential/State Machine hybrid.
Workflow Designers
The first thing you notice with the designers is that they are now written in WPF! This gives a whole host of advantages to the Designer experience, first off you get all the standard benefits of a WPF application (zooming, scaling, etc) but more importantly it is far more responsive that the old 3.x designer. This has a knock on effect on how you can use Activities on the designer, but we'll come onto that later. Lets have a look at the Sequential Designer.
The designer hasn't changed much from its transition to 3.x, you still insert activities in a vertical line onto it's surface. But what has changed are the addition of a couple of very small innocuous buttons in the bottom left of the designer called "Arguments" and "Variables".
Arguments are the method into which you pass values from the outside world into the Workflow you'll be executing, or from a Workflow into an Activity. Previously in 3.x to pass Arguments into a Workflow this meant dropping into code and adding a number of Dependency Properties in by hand.
So how do you add Arguments to a Workflow in 4.0? Easy, you click the Arguments button and add each Argument into the dialog that appears.
Give the Argument a name, select a Direction, a Type and if required a Default value. Once complete, you’ve created your first Argument.
As you can see, we have the choice of assigning a direction to the Arguments. This does exactly as you'd expect, so values are either taken In (like passing by reference), In Out (like passing by Value), Out (return a value) or as a Property.
But how do you actually get the Arguments into a Workflow? This hasn't changed at all, you still pass in a Dictionary with string keys for each of the values. The key relates to the name of the Argument it will be assigned to.
The Variables functionality is new in WF 4.0 and designer to solve a problem of complexity from 3.x. In 3.x you would have activities binding to each other, but this could lead to some very odd behaviour if the activities bound to the wrong activity and make things complex as an Activity could bind to another activity from either end of a Workflow.
So what the WF team introduced in 4.0 is the concept of variables within a Workflow. Instead of binding Activities to other Activities we now have a broker in the middle to hold the value. It sounds terrible and a bit like a global variable, but it does work very well and is only scoped to the parent Workflow or child Activity, so it's a lot more like Members in a Class. You could almost go as far to say the Workflow is now stateful, as it contains values that change as it executes.
So what does the Variables dialog look like?
To create a new Variable supply a Name, Type and if needed a default Value.
Ok, so what about when we change the context of the Workflow designer? What happens then? Well in that case the Variables window will show all of the Variables available to the selected Activity from the parent Activity (or Workflow) and above. Here you can see that the child Sequence Activity has 2 Variables available to it. AVariable owned by the parent Sequence Activity and it’s own AScopedVariable.
Create your first Sequential Workflow
The following is a textual version of the screencast.
Ok, so lets build a Sequential Workflow using these new features in a Console Application. This Sequential Workflow is going to greet a person we name. Now I'm going to be using a number of custom activities to perform the work, but I won't be going into how they work as this will be covered in a future article.
The process I'll be following is quite simple, the User will be welcomed and then prompted for their name and a greeting will then be displayed.
The first thing we need to do with our Workflow is to add in the Activities, so I'll add in a single WriteLine Activity that write text out to a Console, a custom ReadLine Activity that reads in from the Console, the Greeting activity. I do this in exactly the same manner as we add Activities currently, by dragging them onto the surface in the order I desire.
In the next article, I'll be looking at the new Flowchart Workflow and Designer.