Microsoft Virtual Labs

®

Longhorn Server: Windows Workflow Foundation

Longhorn Server: Windows Workflow Foundation

Table of Contents
Longhorn Server: Windows Workflow Foundation.................................................................. 1
Exercise 1 Creating a Custom Activity..........................................................................................................................2 Exercise 2 Use Custom Activity in a Workflow..........................................................................................................10 Exercise 3 Workflow Runtime Services ......................................................................................................................15 Lab Summary ..............................................................................................................................................................19

Longhorn Server: Windows Workflow Foundation

Longhorn Server: Windows Workflow Foundation
Objectives
After completing this lab, you will have: Been exposed to Windows Workflow Foundation, and been provided some hands-on experience working with the workflow tools, programming model and configuration. Built a custom activity to extend the existing workflows. In this lab, you will get an introduction to Windows Workflow Foundation by examining, modifying and extending the workflow components of the DinnerNow sample solution. You will create custom activities, manage workflow persistence and tracking, and take some time to understand how Windows Workflow Foundation was applied in this application. The project files for this lab are in the c:\Dinnernow\labs\server\WF\start folder. 90 Minutes

Scenario

Estimated Time to Complete This Lab Computer used in this Lab

LonghornServer

Page 1 of 19

Longhorn Server: Windows Workflow Foundation

Exercise 1 Creating a Custom Activity
Scenario
In this exercise, you will create a custom activity to manage some cooking equipment at the restaurant. You will create a basic activity which configures the equipment for an order as well as a composite activity that encapsulates this functionality along with other work to be done. Tasks
1. Create a basic

Detailed Steps Note: Prior to beginning this lab, you need to configure the environment and the solution correctly. The steps below will help you execute a script which will configure the projects for you. a. Open Windows PowerShell from Start | All Programs | Windows PowerShell b. Enter the following command to execute the setup script, and then close Windows PowerShell.
C:\Dinnernow\labs\server\WF\start\setup.ps1

activity

Note: In this task, you will create the activity which will simulate controlling a piece of hardware in the restaurant kitchen. This is activity type is known as a basic or simple activity. Basic activities are activities that do some unit of work. In the next task you will create the other type of activity: composite. c. Navigate to c:\Dinnernow\labs\server\Start\src\ and double-click the DinnerNow - OrderProcessing.sln file to open the starter solution.

d. In Solution Explorer, right-click the DinnerNow.OrderProcessing.Workflow.Activities project and choose Add | Activity. e. Select the Visual C# Project Items | Activity template, name the activity “FryMachine.cs” and click the Add button. f. On the design surface, highlight the activity and, in the property browser, change the base class from SequenceActivity to Activity. files. Note: You will need to browse to the System.Workflow.ComponentModel assembly to find the Activity class.

Page 2 of 19

Longhorn Server: Windows Workflow Foundation Tasks Detailed Steps

g. Right click the DinnerNow.OrderProcessing.Workflow.Activities project and choose Add | Existing Item. h. Browse to c:\Dinnernow\labs\server\WF\resources and choose the FryMachineTemperature.cs and FryMachineTemperatureTypeConverter.cs files, and then click Add. Note: These files contain the data type for defining temperature and a type converter to support editing in the property dialog. Examine the files to see how the type converter is related to the type. i. In Solution Explorer, right-click the FryMachine.cs file and choose View Code. j. Add a dependency property to the activity which defines the temperature at which the food needs to be fried. Place the cursor within the class definition and type “wdp” and then press Tab twice to initiate the Dependency Property code snippet. k. Enter “CookingTemperature” for the property name; “FryMachineTemperature” for the property type; “The temperature at which the food should be cooked” for the description; and “Fry-o-lator” for the category. Press Enter when all the values are in to end snippet expansion editing. The code should look like that shown below. Note: Dependency Properties are a core part of Windows Workflow Foundation and serve as the basis for many of the features including activity binding (binding property values on one activity to the properties on another), state management, and property change notification.
public static DependencyProperty CookingTemperatureProperty = System.Workflow.ComponentModel.DependencyProperty.Register ( "CookingTemperature", typeof(FryMachineTemperature), typeof(FryMachine)); [Description("The temperature at which the food should be cooked")] [Category("Fry-o-lator")]

Page 3 of 19

Longhorn Server: Windows Workflow Foundation Tasks Detailed Steps
[Browsable(true)] [DesignerSerializationVisibility( DesignerSerializationVisibility.Visible)] public FryMachineTemperature CookingTemperature { get { return ((FryMachineTemperature)(base.GetValue( FryMachine.CookingTemperatureProperty))); } set { base.SetValue( FryMachine.CookingTemperatureProperty, value); } }

l. Override the Execute method and have it return ActivityExecutionStatus.Closed. Note: The execute method is where basic activities such as this one, do their work. If we were to actually control a piece of hardware, this is where we would write the code to interact with the hardware.
protected override ActivityExecutionStatus Execute( ActivityExecutionContext executionContext) { return ActivityExecutionStatus.Closed; }

m. In Solution Explorer, right-click the DinnerNow.OrderProcessing.Workflow.Activities project and choose Build. Fix any compiler errors before proceeding.
2. Create a composite

activity

Note: In this task, you will be creating a composite activity which will include the basic activity from the previous task and one of the base activities included in Windows Workflow Foundation. A composite activity gets work accomplished by managing its child activities. Composite activities provide for higher level abstractions or encapsulation of business logic and allow you to reuse functionality from basic activities. In this task you will derive from the Sequence activity and let it do the majority of the work. It is possible, however, to create your own composite activities and control the work of child activities directly. a. Right-click the DinnerNow.OrderProcessing.Workflow.Activities project in the Solution Explorer and choose Add | Activity. b. Choose the Visual C# Project Items | Activity template, enter “InitiateFryMachineCooking.cs” as the activity name and then click Add. The resulting designer should appear as shown below.

Page 4 of 19

Longhorn Server: Windows Workflow Foundation Tasks Detailed Steps

Note: When building this type of composite activity, Windows Workflow Foundation provides the Activity Designer which is much like the workflow designer. This allows you to easily define your composite activity by dragging and dropping other activities on the design surface. c. From the toolbox, drag the FryMachine activity onto the design surface where indicated by the text Drop Activities Here. d. Next, drag a Code activity from the toolbox and add it to the designer just below the FryMachine activity. Your activity should look similar to the one shown below.

Note: Note the smart tag on the code activity indicating a problem with the activity as it is currently configured. In this case it is notifying us that the event handler has not been configured for this activity. For your custom activities, you have the ability to write an Activity Validator which can provide similar types of feedback based on your requirements. e. Select the fryMachine1 activity in the designer and review the properties in the property window. Expand the CookingTemperature property and notice that it is showing up as complex property allowing us to set the individual properties of the FryMachineTemperature struct.

Page 5 of 19

Longhorn Server: Windows Workflow Foundation Tasks Detailed Steps

f. In the properties dialog box, double-click the blue icon next to CookingTemperature to invoke the activity bind dialog box. g. Choose the Bind to a new member tab and enter “InitialTemperature” for the property name. Leave the type of member set to Create Property, and click OK.

h. Review the CookingTemperature property in the properties dialog again and notice that instead of information about our type, we have information about the activity binding including the activity and member that this property will now get its value from.

Page 6 of 19

Longhorn Server: Windows Workflow Foundation Tasks Detailed Steps

Note: Two things have happened here. First, we have bound the CookingTemperature property of the basic activity to the InitialTemperature property of the composite activity. Activity binding allows data to be declaratively passed between activities, bringing data binding to components and not just user interfaces. Second, because the child activities of this composite activity are not going to be accessible to users of the composite activity, we needed a way to allow someone using this composite activity to set the temperature. By binding the properties as we have, we have “promoted” the temperature property up a level so that is accessible to users to the composite activity. i. Double-click the codeActivity1 activity on the design surface to generate an event handler for the ExecuteCode event. You should be taken to the code view for the composite activity where you can provide the code for the event handler. j. Enter the code below in the event handler to provide tracking data about what is happening in the composite activity.
CodeActivity act = sender as CodeActivity; FryMachine fryer = (FryMachine)act.Parent.GetActivityByName( "fryMachine1"); this.TrackData("FryMachineTrace", String.Format("FryMachine has started at {0}", fryer.CookingTemperature));

Note: There are two things to notice in this code. First, we are accessing the other activity in the composite by navigating the hierarchy of activities rather than simply using the variable that references it. This is due to how some activities, such as the While activity, manage their child activities. By using this model, we are sure to get the right instances of the activities and not a template instance. Secondly, we are calling the TrackData method from the base activity. In Exercise 3, we will discuss the tracking system in Windows Workflow Foundation in more detail and see how we can consume this data when the workflow is running or completed. For now, remember that we are requesting some data to be tracked from within the code activity event handler. k. Save all files and build the solution fixing any compiler errors or typing mistakes.
3. Create an activity

Note: As you may have noticed, when we added the FryMachine activity to the design

Page 7 of 19

Longhorn Server: Windows Workflow Foundation Tasks designer Detailed Steps surface, it was a plain white activity with a stock icon. The code activity, on the other hand, had custom coloring, an icon, and validation logic. When building an activity, the properties and execution logic reside in the activity class itself, but you also have the ability to create a rich design-time experience. In this task you will create a designer and designer theme and associate them with your activity in order to customize the look and feel. a. Right-click the DinnerNow.OrderProcessing.Workflow.Activities project in Solution Explorer and choose Add | Class. b. Choose the Visual C# Project Items | Class template, name the class “FryMachineDesigner.cs” and click Add. c. Add using statements to the file for System.Workflow.ComponentModel.Design and System.Workflow.Activities.
using System.Workflow.ComponentModel.Design; using System.Workflow.Activities;

d. Make the class public and have it derive from ActivityDesigner.
public class FryMachineDesigner : ActivityDesigner

e. Within the class definition, type “override” and then press the spacebar. This should give you a drop down list of all the methods and properties available on the base class that you can override to customize how your activity behaves in the designer. f. Choose CanBeParentedTo from the list of available overrides and press Enter. g. Enter the following code as the implementation of the method.
return parentActivityDesigner.Activity is EventDrivenActivity || parentActivityDesigner.Activity is StateInitializationActivity;

Note: In this case, we have now indicated that an activity with this designer associated to it can only be added to an event driven activity or a state initialization activity. Later, we will see how this behavior surfaces. h. Repeat steps a through c, but name the class “FryMachineDesignerTheme.cs”. i. Add additional using statements for System.Drawing and System.Drawing.Drawing2D. j. Make the class public and have it derive from ActivityDesignerTheme.
public class FryMachineDesignerTheme : ActivityDesignerTheme

k. Add a constructor to the theme where you initialize the colors and background style. [Snippet: DinnerNow Lab 02 – Exercise 01 – DesignerTheme]
public FryMachineDesignerTheme(WorkflowTheme theme) : base(theme) { BackColorEnd = Color.BlanchedAlmond; BackColorStart = Color.BurlyWood;

Page 8 of 19

Longhorn Server: Windows Workflow Foundation Tasks Detailed Steps
BackgroundStyle = LinearGradientMode.BackwardDiagonal; ForeColor = Color.DarkGreen; }

Note: Using the DesignerTheme gives us basic control over colors and shading. However, the designer class itself contains methods with more explicit control such as the OnPaint method where you can custom draw your activity. Themes allow your activity to have a default look and feel, but they also allow the control to be customized by the user. You can create custom themes and then apply them overwriting the original theme to suit your preferences or needs. To get started, choose Tools | Options in Visual Studio 2005 and then review the Workflow Designer options and create your own themes. l. Now that you have created a DesignerTheme and Designer, you need to associate them with the activity. Start by adding the Designer Attribute to the FryMachine class definition in the FryMachine.cs file.
[Designer(typeof(FryMachineDesigner),typeof(IDesigner))] public partial class FryMachine: Activity

m. Next, add an ActivityDesignerTheme attribute to the FryMachineDesigner class in the FryMachineDesigner.cs file.
[ActivityDesignerTheme(typeof(FryMachineDesignerTheme))] public class FryMachineDesigner : ActivityDesigner

n. Also add the designer attribute to your composite activity in the InitiateFryMachineCooking.cs file so that is shows up as a simple activity on the designer.
[Designer(typeof(FryMachineDesigner),typeof(IDesigner))] public partial class InitiateFryMachineCooking: SequenceActivity

Note: By default, custom composite activities such as the one we have created here show up in the workflow with their child activities visible but locked. In an effort to truly encapsulate our work, we use the activity designer to hide the implementation details from the consumer of our composite activity. o. Build your solution and fix any compiler errors. In the next exercise, you will use the activities in a workflow.

Page 9 of 19

Longhorn Server: Windows Workflow Foundation

Exercise 2 Use Custom Activity in a Workflow
Scenario
In this exercise, you will be using the activities created in the previous exercise within the order processing workflows. You will get a chance to use the workflow designer and see how to take further advantage of parameter binding. Tasks
1. Developing

Detailed Steps Note: In this task, you will add the InitiateFryMachine activity to the workflow which is responsible for processing restaurant orders. You will also bind the temperature property to a property on the workflow. a. In Solution Explorer, open the ProcessRestaurantOrder.xoml file from the DinnerNow.OrderProcessing.Workflows2 project. Note: This workflow is a State Machine workflow, one of the two workflow types that ship with Windows Workflow Foundation. The other type, sequential, processes activities in a defined pattern, whereas the State Machine workflow is event driven and therefore more dynamic. Note: Notice that the workflow is made up of State activities, each defining a different state that the workflow (and the order) can be in. Within each of these states there are other activities that represent events as well as initialization steps. b. Double-click the OrderPickedUp item in the OrderReadyForPickup state.

workflows

Page 10 of 19

Longhorn Server: Windows Workflow Foundation Tasks Detailed Steps

Note: Notice that you are now editing the particular event sequence for the state. There is a breadcrumb navigation system at the top of the designer to get you back to the workflow itself when you are done editing the sequence of activities. The sequence begins with an event and then proceeds with activities performing their work until the sequence completes with a SetState activity to control the transition to the next state. c. Click the ProcessRestaurantOrder link to return to the workflow.

Page 11 of 19

Longhorn Server: Windows Workflow Foundation Tasks Detailed Steps

d. Double-click the InitializeOrderCooking item in the OrderCooking state. Note: Note that this sequence is defined as a StateInitialization activity. This sequence of activities does not start with an event and is run every time the state is transitioned into. There is also a StateFinalization activity that can be used in much the same way to execute activities each time the state is transitioned from. e. From the toolbox, drag the InitiateFryMachineCooking activity and add it as the second activity in the sequence. f. Look in the property browser and notice that the InitialTemperature property is available on composite activity. This property value will be passed, through activity binding, to the FryMachine activity contained within the composite. g. Double-click the blue icon next to InitialTemperature to invoke the activity bind dialog box. h. Choose the Bind to a new member tab and enter “FryerTemperature” for the property name. Leave the type of member set to Create Property, and click OK.

Page 12 of 19

Longhorn Server: Windows Workflow Foundation Tasks Detailed Steps

Note: At this point, you have created a property on the workflow named FryerTemperature. In the next task, you will see how this value can be passed into the workflow. i. Build the solution and fix any compiler errors.
2. Passing parameters

to a workflow

Note: In this task, you will see how to pass parameters into a workflow. Any public property on a workflow class definition can be set by passing a set of named parameters to the CreateWorkflow method. In the DinnerNow application, there are two workflows: the ProcessOrder workflow which handles the main order, and the ProcessRestaurantOrder workflow which handles the individual orders for each restaurant included in the order. We have included our FryMachine activity in the ProcessRestaurantOrder workflow which is initiated from the ProcessOrder workflow. a. Open the OrderService.cs file in the DinnerNow.OrderProcessing.Services project. b. Navigate to the IOrderingService.PlaceOrder method. Note: This method creates the ProcessOrder workflow passing in several parameters using a dictionary object. The names of the entries in the dictionary match the names of properties on the workflow. c. Find the ProcessOrder.xoml file in the DinnerNow.OrderProcessing.Workflows project and expand it to see the code behind files. d. Open SplitAndManageOrder.xoml.cs. Review the public properties defined on the workflow class. Notice that there is nothing special required in the property definition in order to make them parameters for the workflow. e. Close Visual Studio. f. In Windows Explorer, navigate to c:\Dinnernow\labs\server\Start\src\ and double-click the DinnerNow - OrderProcessing.sln file to re-open the solution. g. In Solution Explorer, double-click the ProcessOrder.xoml file in the DinnerNow.OrderProcessing.Workflows project. h. In the designer, find the activity named invokeWorkflowActivity2 in the middle

Page 13 of 19

Longhorn Server: Windows Workflow Foundation Tasks Detailed Steps of the workflow and select it. It contains the representation of the ProcessRestaurantOrder state machine workflow. i. In the property browser, find the parameters for the workflow, and enter “400 Farenheit” for the FryerTemperature property. j. Build the solution and fix any compiler errors.

Page 14 of 19

Longhorn Server: Windows Workflow Foundation

Exercise 3 Workflow Runtime Services
Scenario
In this exercise, you will be exposed to the persistence and tracking services that are part of Windows Workflow Foundation. You will see how the persistence service is used to provide reliability and state management and how tracking provides visibility into the running processes. Tasks
1. Configuring runtime

Detailed Steps Note: In this task, you will see how the SQL persistence service is used in the DinnerNow application. a. Expand the DinnerNow.OrderProcessing.ConsoleHost project in Solution Explorer and open the App.config file. b. Navigate to the bottom of the file and find the WorkflowRuntimeConfig section. Note: Notice that the primary purpose of this configuration section is to configure the runtime services that support the core workflow runtime. Adding services in the configuration is often the best choice for flexibility, but services can also be added in code. Note: The first entry in this section adds the persistence service to the runtime and provides input to the service about how it should behave. c. Add the following configuration entry to configure the SQL Tracking Service as well. (Remove line breaks if copying). <add type="System.Workflow.Runtime.Tracking.SqlTrackin gService, System.Workflow.Runtime, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" ConnectionString="Initial Catalog=DinnerNowWF; Data Source=DinnerNow;Integrated Security=true;" IsTransactional="false" UseDefaultProfile="true"/> Note: In addition to the connection string, we have also configured two other properties of the service. The IsTransactional property indicates whether the tracking data should be stored as soon as it is collected, or only in the context of a transaction. Here we have indicated that the data should be stored immediately. This gives quicker access to the data, but does mean that the tracking data might not match the state of the workflow. It also means that there will be an additional performance overhead as tracking calls are written to the database as they happen. The second property, UseDefaultProfile, tells the service that if a tracking profile for the current workflow type has not been deployed, it should use the default profile about what to track Note:With that configuration, you have added the tracking service to the runtime.

services

2. Creating a tracking

profile

Note: In this task, you will define a tracking profile for the ProcessRestaurantOrder workflow to collection specific data. In particular, you will create a profile to extract the temperature property from the FryMachine activity as well as tracking the custom

Page 15 of 19

Longhorn Server: Windows Workflow Foundation Tasks Detailed Steps data that was written using the Code activity. a. In Windows Explorer, open the c:\Dinnernow\labs\server\WF\resources directory, and find the TrackingProfileDesigner.exe, TrackingProfileDesigner.exe.config, and WorkflowDesignerControl.dll files. b. Copy all three files to the c:\Dinnernow\labs\server\Start\bin\services directory. Overwrite the existing files. Note: We copy these files so that when you load the workflow, all of the dependent assemblies are in the current directory with the application. c. Double-click the TrackingProfileDesigner.exe to start the application. d. Choose File | Open | Workflow From File and choose the DinnerNow.OrderProcessing.Workflows2.dll assembly in c:\Dinnernow\labs\server\Start\bin\services, and click Open. Note: The tracking profile designer is an application that hosts the workflow designer control and uses it to allow for creating tracking profiles visually. The tracking profile tells the tracking interceptor which events the tracking service is interested in. The tracking profile designer is part of the Windows SDK, where you can find the full source code for this tool.

Note: Notice that the workflow does not have the same layout in the designer here as it did in the Visual Studio designer. The State Machine Workflow designer is based on a Free Form Designer which uses a layout file to store information about the placement of activities. That layout file can be used when re-hosting the designer to make sure the workflow looks as it did when created. e. Double-click the InitializeOrderCooking state initialization in the OrderCooking state. f. Highlight the initiateFryMachineCooking1 activity. g. In the toolbar, click the button labeled Track InitateFryMachineCooking.

Page 16 of 19

Longhorn Server: Windows Workflow Foundation Tasks Detailed Steps

Note: Notice that the designer updates to provide guidance as you build the profile. This is one example of the possibilities when rehosting the visual workflow designer. The activity shows the pushpin indicating that the activity is being tracked and another glyph to indicate that the activity tracking is not completely configured for the activity. h. In the toolbar, select Activity Events | Closed. i. In the toolbar, select Extract Data | InitiateFryMachineCooking | InitialTemperature. Note: You have defined a tracking profile to track when the InitiateFryMachineCooking activity in this workflow is completed or closed. In addition, you have defined a data extraction to pull the value of the temperature property at this time and track it. j. Click the tab at the top of the designer labeled Tracking Profile Markup and review the XML that represents the tracking profile you have created. k. Choose File | Save | Profile as File and enter “TrackingProfile.xml” for the file name. l. Click on Save, and click Yes to replace the existing file. m. Close the Workflow Tracking Profile Designer application. n. Click Start and enter “devenv.exe c:\Dinnernow\labs\server\Start\bin\services\trackingprofile.xml” to open the TrackingProfile.xml file in Visual Studio 2005. o. Add the following configuration just after the closing tag </ActivityTrackPoint>. [Snippet: DinnerNow – Lab02 – Exercise 03 UserTrackingLocation]
<UserTrackPoint> <MatchingLocations> <UserTrackingLocation> <Activity> <Type>System.Workflow.Activities.CodeActivity, System.Workflow.Activities, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35</Type>

Page 17 of 19

Longhorn Server: Windows Workflow Foundation Tasks Detailed Steps
<MatchDerivedTypes>true</MatchDerivedTypes> </Activity> <KeyName>FryMachineTrace</KeyName> <Argument> <Type>System.Object, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</Type> <MatchDerivedTypes>true</MatchDerivedTypes> </Argument> </UserTrackingLocation> </MatchingLocations> </UserTrackPoint>

Note: Because the sample Tracking Profile Editor does not support user track points, we have to add that to the file manually. Tracking profiles can also be created in code as opposed to XML. p. Save the file and close Visual Studio. q. Double-click the ResetTracking.bat file in C:\Dinnernow\labs\Server\ to reset the tracking workflow database and provide a clean starting point. r. Double-click the TrackingProfileDesigner.exe in c:\Dinnernow\labs\server\Start\bin\services\. s. Choose File | Open | Workflow From File and choose c:\Dinnernow\labs\server\Start\bin\services\DinnerNow.OrderProcessing.Wo rkflows2.dll, and click Open. t. Open TrackingProfile.xml by choosing File | Open | Profile From File and selecting c:\Dinnernow\labs\server\Start\bin\services\TrackingProfile.xml, and click Open. u. Choose File | Save | Profile to SQL Tracking Database. You should see a message box that indicates the profile was saved successfully. v. Close the Workflow Tracking Profile Designer. w. Click the “END LAB NOW” button in the lab console.

Page 18 of 19

Longhorn Server: Windows Workflow Foundation

Lab Summary
In this lab, you learned about building workflows and activities to define business processes. You also saw how easy it was to extend and customize both the design-time environment and the host environment.

Page 19 of 19

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer: Get 4 months of Scribd and The New York Times for just $1.87 per week!

Master Your Semester with a Special Offer from Scribd & The New York Times