Windows Workflow Foundation (WF) is a very cool framework whose main focus is to help orchestrate a business process or several steps. If you still haven't read or understand what WF is, I suggest you do some reading. The purpose of this post is to help you create your first marketable activity $$$. When I say marketable, I mean an activity that not only works functionally but also looks great inside the Workflow designer. A good activity would include things like visual design, reusable and atomic functionality, and validation. This post will be documenting the creation of an activity whose main purpose is to beep; yes you read that right, beep. I know it is not exactly mind blowing functionality but it will keep you from getting hung up on the activity's function and focus more on the architecture surrounding an activity. When we are done we will have an activity that looks something like the image you see below. Excited yet? Well I hope you are. I also hope that you are thinking "that is a pretty sweet looking activity," and you are wondering how to accomplished creating a smiliar looking activity. Sit down for a while, kick up your feet, put your pipe jacket on and prepare to get some knowledge dropped on you.
MonstersGotMyWorkflow.zip (881.67 kb) (Part I, covers only this post)
Setting Up The Project
The first step is to create a new WF project. Start VS2008 and go to File > New > Project. Under Visual C# select "Sequential Workflow Console Application" and create a new project; call it anything you want. We are creating a sequential workflow console application project because we will use it to test our activity along the way. I know the TDD crowd will jump on me for this, but WF is such a different domain of development that it is not easy to follow practices. John Locke once said something like, when dropped in the jungle with cannibals you shouldn't hold on to the laws of civilization because they will just get you eaten. Holding on to TDD in this scenario will just get you frustrated. I've been told to only test functionality when dealing with WF and to treat an activity like a wrapper; that idea seems to work. Since we are only beeping I feel tests might be overkill.
Now right click your solution and add a new project of type "Workflow Activity Library," call it BeepActivityLibrary.
Excellent, now you should have two projects in your solution, one workflow and one activity library. The next step is to start working on our activity.
Activity Development
The first thing we need to do is change the name of our activity from Activity1 to something more meaningful. double click Activity1.cs and you should see it in design view. Click on the activity and change the name in the Properties window to BeepActivity, also give the activity a description of "Beeps to a user specified frequency and duration." Finally, change the base class of the activity to Activity.
Time to spike and implement some functionality to the activity. Right click the activity in the designer and select "View Code". The Code should look like this.
namespace BeepActivityLibrary
{
public partial class BeepActivity : System.Workflow.ComponentModel.Activity
{
public BeepActivity()
{
InitializeComponent();
}
}
}
We will need to implement the execute method of the activity, it is pretty simple. After implementing the activity execution method your code should look like this.
using System;
using System.Workflow.ComponentModel;
namespace BeepActivityLibrary
{
public partial class BeepActivity : System.Workflow.ComponentModel.Activity
{
public BeepActivity()
{
InitializeComponent();
}
protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{
Console.Beep(500, 1000);
return ActivityExecutionStatus.Closed;
}
}
}
The values for frequency and duration are hard coded for now, but in the next part I will show how to make these configurable through the designer or by binding at runtime. Build the solution and all should go well. Open the Workflow1.cs file in designer mode and you should see the BeepActivity in the toolbox. Drag the beep activity into the workflow and run the application. You are on your way to creating a marketable activity. In the next part, I will show you how to create properties on your activity, style your activity, and validate your activity.