Workflow Foundation Activity Template

WF has two types of activities you can create: the easy one and the hard one. That’s not really their names (though it would be cool)—the first (easy) is a composite activity. This is the one that Visual Studio lets you create, and in effect, it’s just another sequential workflow that you build up and reuse.

image

However, when you use them, they look like sequence activities. If you want something that looks like a “real” (read: what you get out of the box) activity, you’ll need to create the hard one—which is simply called an activity (so clear a name, ain’t it). The smallest this can be is one class, and the biggest it can be is five classes, all with basically the same structure. So it’s weird there’s no easy way to build these—say, with a template. Well, I’ve created one that sets up the base for an activity and lets you get going as quickly as possible.


Usage

To use it, select File → New Project, and under Visual C#, My Templates will include the template called WF Activity. Some things to note from that little sentence:

image

When you create it, it generates four files, all starting with the name you choose (which will also be the name for your activity, so I don’t suggest putting Activity in it). For example, mine was called ConsoleWriteLine, so my files are:

image


Special Features

I’ll come back to what they do, but I wanted to point out that I’ve taken special care with the code so that if you run code analysis (FxCop), it reports no issues—except for the project not being signed and COM visibility stuff. StyleCop also reports no issues. I’ve also added three TODOs (if you use the VS task list):

image

image

image


What do I need to add to use it?

Basically, all you need to do to get started is:

  1. Add the properties you need to the {Project}Activity.cs file.
  2. Add the code as indicated by the task item.
  3. Compile and use.

Optionally, you can:

The text display is controlled via the SetText method in {Project}ActivityDesigner.cs and has been wired up to respond to property changes. So you can have the text update dynamically if the properties change with minimal code.


How do I deploy it?

To use it, simply dump the ZIP file in: Visual Studio 2008 > Templates > ProjectTemplates > Visual C# in your Documents folder, then restart Visual Studio!

image


Where can I get it?

Download it here

Note: My hosting provider corrupts ZIP files, so it’s uploaded in 7-Zip format—you’ll need to extract it.

Hopefully, you get some productivity benefit from this component or it helps you learn how to write your own. And if it does, please leave a comment so I know (and get that warm fuzzy feeling I like so much).