To get you up and running, this section explains how to create a workflow. The first workflow you are going to create is going to be very simple. You will create a workflow that asks for your name and says hello. This simple workflow uses conditional execution to determine whether your name is Bob. If it is, the workflow will recognize you; if it is not, the workflow will think of you as a stranger. The first thing you need to do is to create a new workflow project by following these steps:
1. Click Start ➤ All Programs ➤ Microsoft Visual Studio 2005 ➤ Microsoft Visual Studio 2005, to open Visual Studio 2005.
2. Click File ➤ New ➤ Project and select the Workflows project type, then choose the fol- lowing template: Sequential Workflow Console Application.
3. Give the workflow the following name: HelloWorkflow. Then click OK.
We have chosen to create a sequential workflow console application because this template generates a workflow scheme that is hosted within a console application, which makes it easy to test our workflow. Since workflows cannot be executed directly, you will need some kind of host to test a workflow.
C H A P T E R 4 ■ W I N D O W S W O R K F L O W F O U N D A T I O N 155
You can pass parameters to a workflow. Those parameters are mapped to workflow prop- erties whose names have to be identical to the parameter names. Under the cover, workflows are represented by classes that inherit from the activity base class located in the
System.Workflow.ComponentModel namespace; so parameters are eventually mapped to class properties.
The default view selected in the workflow project is the workflow design view of a default workflow item that is called workflow1.cs. On the left side of your Visual Studio editor, in the design view, you will see the Toolbox that contains activities related to this type of workflow. Follow these steps to create the example workflow:
1. Drag a CodeActivity from the Toolbox onto the design view.
2. Select the CodeActivity, and in the Properties window, select the Name property and give the CodeActivity the following name: question.
3. Drag an IfElseActivity from the toolbox onto the design view directly below the CodeActivity.
4. Select the IfElseActivity, and in the Properties window, select the Name property and give the IfElseActivity the following name: decideGreeting.
5. Select the left IfElseBranchActivity and in the Properties window, select the Name property and give the IfElseBranchActivity the following name: bobBranch.
6. Select the right IfElseBranchActivity and in the Properties window, select the Name property and give the IfElseBranchActivity the following name: strangerBranch.
7. Drag a CodeActivity from the Toolbox onto the design view below the left IfElseBranchActivity (bobBranch).
8. Select the CodeActivity, and in the Properties window select the Name property and give the CodeActivity the following name: HelloBob.
9. Drag a CodeActivity from the Toolbox onto the design view below the right IfElseBranchActivity (strangerBranch).
10. Select the CodeActivity and in the Properties window, select the Name property and give the CodeActivity the following name: HelloStranger.
Figure 4-7 shows the sequential workflow you have just made. You will notice the presence of several exclamation signs in the workflow. This means that there is some additional infor- mation required to make these activities work.
156 C H A P T E R 4 ■ W I N D O W S W O R K F L O W F O U N D A T I O N
Figure 4-7. Sequential workflow example
Next we are going to add a parameter to the workflow. We can do this by going to the code view of workflow1.cs, right-clicking workflow1.cs, and selecting View Code. You must add the following code:
string strFirstname; public string Firstname {
get { return strFirstname; } set { strFirstname = value; } }
We are going back to the design view of our workflow to add some code to the CodeActivity shapes:
C H A P T E R 4 ■ W I N D O W S W O R K F L O W F O U N D A T I O N 157
1. Double-click the question CodeActivity.
2. Add the following code:
Console.WriteLine("Hi, what is your name?"); Firstname = Console.ReadLine();
3. Go back to the design view of the workflow and double-click the HelloBob CodeActivity.
4. Add the following code:
Console.WriteLine("Hello Bob"); Console.ReadLine();
5. Go back to the design view of the workflow and double-click the HelloStranger CodeActivity.
6. Add the following code:
Console.WriteLine("Hello Stranger"); Console.ReadLine();
The last thing you need to do is to add code to the bobBranch activity. Follow the next steps:
1. Select the bobBranch activity and go to the Properties window.
2. Select System.Workflow.Activities.Rules.RuleConditionReference in the Condition
property.
3. Expand the Condition property and give the ConditionName property the following name: Bob
4. Click the button in the Expression property value; this will open the Rule Condition Editor window (see Figure 4-8).
5. Add the following code: this.Firstname == "bob" and click OK.
Figure 4-8. Rule Condition Editor window
If you want to run the workflow, just hit the F5 button.
158 C H A P T E R 4 ■ W I N D O W S W O R K F L O W F O U N D A T I O N