Professional Documents
Culture Documents
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
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.
C:\Dinnernow\labs\server\WF\start\setup.ps1
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
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.
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
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.
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 Detailed Steps
designer 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.
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.
k. Add a constructor to the theme where you initialize the colors and background
style.
[Snippet: DinnerNow Lab 02 – Exercise 01 – DesignerTheme]
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.
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
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 Note: In this task, you will see how to pass parameters into a workflow. Any public
to a workflow 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.
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.
2. Creating a tracking Note: In this task, you will define a tracking profile for the ProcessRestaurantOrder
profile 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