Skip to main content

WF has two 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 is the one that Visual Studio lets you create and is in effect just another sequential workflow which you build up and reuse.

image

However when you use them they just look like sequence activities. If you want something that looks like a “real” (read: what you get out of the box) activity to need to create the hard one which is call 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 and they all basically have the same structure. So it’s weird that there is no easy way to build these, say a template. Well I have created one which sets up the base for an activity and lets you get going as quickly as possible.

Usage

To use it, select the normal Add -> New Project, and under Visual C#, My Templates will be the template called WF Activity! Some things that should become clear from that little sentence

  • You need Visual Studio 2008 (yeah, that wasn’t clear but I need to sneak it in somewhere).
  • It’s C# only
  • It creates a project for your new activity for ease of packaging purposes.
  • And for some reason if I put a space in the name, if crashes my Visual Studio - so um, that could just be me.

 

image

When you create it, it creates 4 files which all start with the name you choose (that 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:

  • ConsoleWriteLineActivity.cs
  • ConsoleWriteLineActivityDesigner.cs
  • ConsoleWriteLineActivityDesignerTheme.cs
  • ConsoleWriteLineActivityValidator.cs

image 

Special Features

I will come back to what they do but I thought I would point out that I have taken special care with the code so that if you run code analysis (fxcop) you get no issues with the code - you will get five for the project not being signed and com visibility stuff - and that StyleCop also reports no issues. I have also added in three TODO’s (if you use the VS task list)

image

  • The first one (Write Code) takes you to the Execute method in the {Project}Activity.cs file. This is what will run when your activity is executed.
  • The second one (Pick some nice colours) takes you to {Project}ActivityDesignerTheme.cs file where you can specify the start and end colours for the gradient. By default it’s dark to light blue.

image

  • The third one (Add validation) takes you to the Validate method in the {Project}ActivityValidator.cs file where you can add any design time validation you need. It even has a sample of how to add a validation failure message.

image

What do I need to add to use it?

Basically all you need to do to get up and running 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 change the colours, add validation, add an icon or change what is shown on the component. The changing of what is shown is controlled via the SetText method in {Project}ActivityDesigner.cs and it has been wired up so that it will respond to property changes. So you can actually have the text change if the properties change with very little code needed.

How do I deploy it?

To use it just dump the ZIP file in the Visual Studio 2008 > Templates> ProjectTemplates > Visual C# in your documents folder and restart Visual Studio!

image

Where can I get it?

 

Note: My hosting provider has something that corrupts zip files so it has been uploaded in 7-zip format which you will 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 here so I know (and get that warn fuzzy feeling I like so much).