The Workflow Workflow. 4. Hello Workflow. 5. Creating the Project. 6. Declaring
the Workflow. 8. Adding the Sequence Activity. 9. Adding the WriteLine Activity.
ProWF Windows Workflow in .NET 4
Bruce Bukovics
Apress*
Contents
xxxi
About the Author About the Technical Reviewers
Acknowledgments
xxx»
•»
Chapter 1:
A
Quick Tour of Windows Workflow Foundation
Why Workflow? Workflows Are Different
Your
Windows Workflow Foundation?
Development Environment
1 1 2 3
4
The Workflow Workflow
4
Hello Workflow
5
Creating
the
Project
Declaring the Workflow
6 8
Adding
the
Sequence Activity
9
Adding
the WriteLine Activity
10
Hosting
the Workflow
12
Running the Application
14
Exploring
15
the Xaml
Passing Parameters Declaring Hosting
vi
xxx'»
xxxiv
Introduction
Why
—•
the Workflow
the Workflow
17 17 20
CONTENTS
Running the Application
21
Using Argument Properties
21
Making Decisions
22
Creating the Project Implementing
a
23
Custom Activity
23
Defining Arguments
25
Defining Variables
26
Adding the Custom Activity
28
Adding the Switch and Assign Activities
29
Hosting the Workflow
34
the
35
Running
Debugging Unit
Application
the
Application
37
Testing
39
Testing
the Custom
Testing
the Workflow
Activity
39 41
Summary
43
Chapter 2: Foundation Overview WF Features and Declarative
Capabilities
Activity Model
.45 45 45
Standard Activities
46
Custom Activities
46
Workflow Designer
47
Custom
Activity Designers
and Validation
47
Multiple Modeling Styles
47
Workflow
48
Debugger
Workflow Services
48
Multiple
48
Workflow Hosts
Workflow Extensions
49
Persistence
49
vii
CONTENTS
Bookmark
49
Processing
Expressions
50
Transaction Support
50
Compensation Workflow
and
Exception Handling
50
Tracking
Designer Rehosting
51
WF 3.x Migration
51
Assemblies and
Namespaces
51
System. Activities
51
System.Activities.Durablelnstancing
52
System.Runtime.Durablelnstancing
52
System.Activities.Presentation
53
System.ServiceModel.Activities
53
3.x Assemblies
54
Activity Life Cycle
55
Definition
vs.
Definition
vs. Runtime
Runtime Instance Variables
Activity States
55 56 57
Expressions
57
Visual Basic Expressions
58
VB Primer for Workflow
58
Developers
Expression Activities
Missing
4
Features
62
66
State Machine
67
Reuse of WCF Contracts
67
C#
Expression Support
68
to SQLServer
68
Tracking
viii
50
Rules Engine
68
Dynamic Updates
68
CONTENTS
Summary
68
CilctpUfSr 3d ACtlWtlfiS
iBntaDm
nana an
Understanding Activities
«!a
*
nanBeanRaaBtiaaDBflBBBflHiiBBiiita'Ft 71
Authoring Activities
72
Kinds of Work
72
Kinds of Data
74
Activity Class Hierarchy
76
Custom Activity Workflow
78
An
Example Activity
78
Implementing an Activity in Code Creating
the
78 79
Project
Implementing the Activity
79
Implementing Unit Tests
81
Testing the Activity
83
Declaring Creating
an
Activity with Xaml
the
Activity Class
84 84
Defining Arguments
84
Defining Variables
85
Declaring the Activity
85
Implementing Unit Tests
87
Testing the Activity
88
Declaring
an
Activity with Code
88
Creating
the
Activity
Class
89
Implementing the Activity Implementing
Unit Tests
Creating
the
an
Asynchronous Activity
Activity
92 93
Testing the Activity
Implementing
89
Class
93 94
ix
CONTENTS
Activity
Implementing
the
Implementing
Unit Tests
94 96
Testing the Activity
97
Using Activities Workflow
97
Building Blocks
97
Activity Data Flow Variable
99 100
Scoping
Standard Activities
Summary
101
Standard Activities
103
Control Flow
104
Flowchart
105
Messaging
106
Runtime
107
Primitives
107
Transactions and Compensation
108
Collection
109
Error
Management
Handling
109
Migration
110
Summary
110
Chapter 4: Workflow Hosting
111
Understanding the Workflowlnvoker Class
111
Using the Static Methods
111
Using the Instance Methods
112
Using the Workflowlnvoker Static Methods Declaring the HostingDemoWorkflow
114
Simple Hosting of the Workflow
117
Passing Arguments
x
114
with Workflow
Properties
118
CONTENTS
a Timeout Value
Declaring Invoking
a
119
Generic Activity
,
Using the Workflowlnvoker Instance Methods Using
the
InvokeAsync
Using
the
Beginlnvoke Method
Understand the Constructing
Method
WorkflowApplication Class WorkflowApplication
a
Assigning Code
to
Delegate Members
120
122 122
,
124 ,
126 127 127
Managing Extensions
129
Configuring and Managing Persistence
129
Executing
a Workflow
Managing
Bookmarks
Manually Controlling
Instance
131
a Workflow Instance
Using the WorkflowApplication Class Hosting
the Workflow with
WorkflowApplication
Workflow Instance
Canceling
a
Aborting
Workflowlnstance
a
Terminating
a
130
Workflowlnstance
Using the BeginRun Method
132
133 133 137 138 139
140
Understanding the ActivityXamlServices Class
142
Using the ActivityXamlServices Class
143
Invoke Workflows from ASP.NET
....145
Designing the ASP.NET Application
145
Hosting the Workflow
147
Testing the Application
149
Managing Multiple Workflow Instances
150
Implementing
a
Workflow Manager
150
Implementing
the Instancelnfo Class
154
CONTENTS
the User Interface
Designing
Implementing Testing Using
the
154
the User Interface Code
Application
the WPF
159
SynchronizationContext
160
Summary
161
Chapter 5: Procedural Flow Control
163
Understanding
the Procedural
Modeling Style
Making Decisions Understanding
Understanding
163 164
the If
Activity
164
Understanding the Switch Activity the While and DoWhile Activities
164
165
Using the While and DoWhile Activities
167
Implementing the InventoryLookup Activity
167
Declaring the Getltemlnventory
169
Workflow
Hosting the Workflow Testing Using
172
the Workflow
the DoWhile
173
Activity
174
Understanding the Parallel Activity.
176
Understanding Parallel Execution
176
Creating
179
the ParallelDemo
Project
Declaring the ParallelDemo Workflow
179
Hosting the Workflow
179
Testing the Workflow
180
Adding
a
180
Testing
the Revised Workflow
Delay Activity
181
Using the Parallel Activity Creating
the GetltemLocation
Declaring
xii
156
182
Project
the GetltemLocation Workflow
182 182
CONTENTS
Hosting the Workflow
186
the Workflow
187
Testing
Obtaining Asynchronous Execution with the Parallel Activity
189
Implementing the InventoryLookupAsync Activity
189
Modifying
191
the GetltemLocation Workflow
the Revised Workflow
Testing
192
Summary Chapter
194
6:
Collection-Related Activities
195
Understanding the ForEach Activity
195
Understanding the Collection Activities
198
Using the ForEach and Collection Activities
200
Creating
the
ActivityLibrary Project
Implementing
Item Structures
Implementing
the FindlnCollection
Declaring
InventoryUpdate
the
200
200
Activity
Workflow
201 203
Hosting the Workflow
209
Testing the Workflow
210
Using the ParallelForEach Activity Testing the Revised Workflow
Working
with Dictionaries
211 212
213
Implementing the Dictionary-Related Activities
214
Declaring the InventoryUpdateDictionary Workflow
218
Hosting
the Workflow
Testing the
Workflow
••220 221
Understanding the InvokeMethod Activity
222
Using the InvokeMethod Activity
223
Revising
the
Modifying
Itemlnventory Class
the Workflow
224 224
xiii
CONTENTS
Testing the Workflow
226
Summary
228
Chapter 7: Flowchart Modeling Style
229
Understanding the Flowchart Modeling Style
229
Using the Flowchart Modeling Style
230
Flowchart Activity
230
FlowDecision
231
Activity
FlowSwitch Activity
231
FlowStep Activity
232
Putting
It All
232
Together
The Flowchart Workflow
233
Making Simple Decisions
233
Implementing Creating
the
ParseCalculatorArgs Activity
the Console
Defining Arguments
235
Project
and Variables
235
Declaring the Workflow
236
Hosting the Workflow
239
Testing the Workflow
240
Declaring Looping Behavior Implementing Creating
the
InventoryLookup Activity
the Console
Defining Arguments Declaring
Project and Variables
the Workflow
Testing the Workflow
Declaring
241 241 243 243 243
Hosting the Workflow
xiv
234
247 ,
Custom Activities
247
248
Defining Arguments and Variables
248
Declaring the Activity
249
CONTENTS
Unit Tests
Implementing
253
Testing the Activity
Mixing the Two Styles Implementing
251
Item Structures
254 254
Implementing the FindlnCollection Activity
255
Creating the Console Project
256
Defining Arguments Declaring the
and Variables
Workflow
256 257
Hosting
the Workflow
261
Testing
the Workflow
262
263
Summary
Chapter
8: Host Communication....
The Need for
Long-Running Workflows
265
265
Understanding Bookmarks
266
Using Bookmarks
268
Implementing
the
GetString Activity
268
Implementing
the
ParseCalculatorArgs Activity
269
Creating
Project
the Console
270
the Workflow
273
Testing the Workflow
275
Hosting
Understanding Workflow Extensions
276
Using Workflow Extensions
278
Declaring the Extension Interface
279
Implementing the HostEventNotifier Extension
279
Implementing the NotifyHost Activity
280
Declaring
the BookmarkCalculatorExtension Workflow
281
Hosting
the Workflow
283
Testing
the Workflow
285
xv
CONTENTS
Using
Alternate Extension
an
Implementing
the HostQueueNotifier Extension
287
Hosting
the Workflow
288
Testing
the Workflow
289
Understanding the ActivityAction
290
Using the ActivityAction
293
Implementing
the
NotifyHostWithAction Activity
293
Declaring the BookmarkCalculatorAction Workflow
294
Binding the Action Property
297
Hosting the Workflow
297
the Workflow
300
Testing Using
the InvokeAction
Activity
300
Understanding the Pick Activity
302
Using the Pick Activity
303
Implementing
Creating
the WaitForBookmark Activity
the Console
Project
303 304
Defining Variables
304
Declaring the ProblemReporting Workflow
304
Hosting
the Workflow
309
Testing
the Workflow
311
Summary Chapter
312
9: Workflow Services
..313
Introducing Workflow Services
313
Understanding WCF
314
Defining
Service Contracts
Configuring Endpoints Hosting
xvi
286
and
and
Configuration
315
Bindings
316 316
CONTENTS
Understanding Workflow Services
,
Messaging Activities Service Contracts and
317
Message Types
322
Correlation
324
Declaration and
Hosting Options
326
Controlling Workflow Service Instances
Declaring
Workflow Service
a
Tasks for
317
327
,
Request/Response Operation
a
Implementing
the
OrderProcessing Workflow Service
327 327
328
Creating the ServiceLibrary Project
329
Implementing Request and Response Classes
329
Declaring the Service Operation
332
Populating the Response
337
Configuring Testing
the Service
the Service
Publishing
a
Web.config
the
Publishing
to IIS
Implementing a
339
Workflow Service to IIS
Enhancing
Adding
339
a Client
Application
Invoking the Service
Testing
the
Configuration
the Client
Implementing
a
Application
Workflow Client
Implementing Custom Activities Adding
a
341 342
Service Reference
Reviewing
341
Service Reference
342 343 343
346 347
348 349 350
Implementing the InitiateOrderProcessing Workflow
351
Hosting the Workflow
359
Testing
the Client
Application
359
xvii
CONTENTS
Self-hosting the Workflow
Service
361
,
Understanding the WorkflowServiceHost
361
Tasks for Self-hosting
362
a
Service
Implementing the Sen/iceHost Application
363
Configuring
the Service Host
366
Testing the
Self-hosted Service
367
Using the
Workflow/Client Application
367
368
Summary Chapter
10: Workflow Services Advanced
Topics
Using Context-Based Correlation
369
Guidelines for Context-Based Correlation
Declaring the ShipOrder Workflow Service
370 ,
Modifying the OrderProcessing Service Hosting
the
Configuring Testing
ShipOrder
Workflow Service
the ServiceHost
the Revised
Application
OrderProcessing
Workflow
Using Content-Based Correlation
371 379 384 384 385
386
Guidelines for Content-Based Correlation
386
Modifying
386
the
Configuring Testing
ShipOrder Workflow
the ServiceHost
Service
Application
the Revised Workflow Service
Implementing
xviii
369
a
Duplex Message Exchange Pattern
388 388
388
Guidelines for the Duplex Message Exchange Pattern
389
Declaring the CreditApproval Workflow Service
390
Modifying the OrderProcessing Service
394
Hosting the CreditApproval Workflow Service
400
Configuring the ServiceHost Application
400
Testing the Revised Workflow Service
402
CONTENTS
Using
a
405
Custom Workflow Extension
Implementing
the
406
OrderUtilityExtension
Implementing the GetOrderld Activity
406
Modifying the OrderProcessing
407
Adding
Workflow Service
the Extension
409
Testing the Revised Workflow Service
409
Understanding Exceptions and Faults
409
Flowing Transactions into a Workflow Service
411
Using Standard Behaviors
412
Summary
414
Chapter 11: Workflow Persistence
415
The Need for Workflow Persistence
415
Understanding Workflow Persistence
416
Instance Stores
416
Actions that Trigger Persistence
417
Understanding Durable Delay
418
Preventing Persistence
419
Persisted Data and Extension Mechanisms
419
Understanding WorkflowApplication Persistence
419
Understanding the SqIWorkflowlnstanceStore
421
Using the SqIWorkflowlnstanceStore with WorkflowApplication
424
Creating
the
ActivityLibrary Project
424
Implementing the Item-Related Classes
425
Implementing the Custom
426
Extension
Implementing Activities that
use the Extension
,
428
Implementing Bookmark-Related Activities
429
Declaring the OrderEntry
431
Hosting
and
Workflow
Persisting the
Workflow
435
xix
CONTENTS
Configuring the Application
440
Testing the Application
441
Understanding WorkflowServiceHost
Persistence
Using the SqIWorkflowlnstanceStore with WorkflowServiceHost Declaring
the
OrderEntryService Workflow
the Workflow Service
Hosting
Testing the ServiceHost Project
444 445 452 457
a
Client
Project
458
Configuring the
Client
Project
464
Implementing
Testing the
Client
Project
Chapter
12:
Customizing Workflow Persistence
Understanding the PersistenceParticipant Classes The
465 467
Summary
PersistenceParticipant Class
469 469 470
The PersistencelOParticipant Class
471
Which Class to Use?
471
Using the PersistenceParticipant Class Modifying the ItemSupportExtension Class Testing the Revised
Extension
472 472 474
Promoting Properties
474
Using Property Promotion
475
Modifying the ServiceHost
475
Modifying the Client Application....
476
Configuring the Client Application
477
Testing
the Revised
Example
478
Understanding the Management Endpoint
479
Using the Management Endpoint
480
Modifying the ServiceHost Configuration
xx
443
480
CONTENTS
Modifying
the Client
480
Application
482
Configuring the Client Application Testing
the Revised
Implementing
a
Understanding
482
Example
Custom Instance Store
the InstanceStore Class
Understanding the Instance
484
Persistence Commands
Understanding the InstancePersistenceContext Class Implementing
a
File
483
System-Based Instance Store
484 485 485
Implementing the FileSystemlnstanceStorelO Class
492
Modifying the ServiceHost Project
501
Testing the Custom Instance Store
502
Summary
505
Chapter 13: Transactions, Compensation, and Exception Handling
507
Understanding Default Exception Handling
507
Implementing
508
the
Example Workflow
Enabling LINQ
Access to the AventureWorks Database
510
Implementing
the GetOrderDetail
511
Implementing
the
Activity
UpdateProductlnventory Activity
514
Implementing the InsertTranHistory Activity
516
Implementing the External Update Activity
517
Implementing the DisplayProductlnventory Activity
518
Declaring
Updatelnventory Workflow
520
Declaring the Displaylnventory Workflow
523
Hosting the Workflow
526
Testing
the
the Workflow
528
Understanding the TryCatch Activity
530
Using the TryCatch Activity
531
Declaring
the
UpdatelnventoryTryCatch
Workflow
532
XXL
CONTENTS
Hosting the Workflow
535
Testing the Workflow
536
Catching Multiple Exceptions
537
Testing
the Revised Workflow
539
Understanding the TransactionScope Activity
540
Using the TransactionScope Activity
541
Declaring the UpdatelnventoryTran Workflow Workflow
545
the Workflow
545
Hosting the Testing
Using
a
Host Transaction
546
Hosting
the Workflow
547
Testing
the Workflow
548
Understanding Compensation
549
Using the CompensableActivity
551
Implementing the ExternalVoid Activity Declaring Hosting
the
UpdatelnventoryComp
551
Workflow
552
the Workflow
555
Testing the Workflow
555
Manually Triggering Compensation Declaring the UpdatelnventoryManualComp
557 Workflow
557
Hosting the Workflow
562
the Workflow
562
Testing
xxii
542
Understanding the CancellationScope Activity
564
Summary
564
Chapter 14: Workflow Tracking
565
Understanding Workflow Tracking
565
Uses of Workflow Tracking
566
Workflow Tracking Architecture
566
CONTENTS
568
Tracking Records Tracking
574
Profiles
578
Tracking Participants
579
Using ETW Workflow Tracking
579
Providing AdventureWorks Access
Copying the Custom Declaring
580
Activities
580
the Workflow
584
the Workflow
Hosting
584
Enabling ETW Workflow Tracking Testing
586
the Workflow
Viewing
the
Tracking
586
Data
591
Using Tracking Profiles Including
592
Selected Workflow Instance States
Including All Workflow Instance States
593
Adding Selected Activity States
594
Targeting Selected Adding
596
Activities
598
Selected Scheduled Records
600
Including Custom Tracking Records
Developing
a
604
Custom Tracking Participant
Implementing the Tracking Record Serializer
605
Implementing the Custom Tracking Participant
608
Testing
the
Developing
a
the
615
Nonpersisting Tracking Participant
Implementing
Testing
611
Tracking Participant
the
615
Tracking Participant
615
Tracking Participant
Using Workflow Tracking with
a
Declarative Service
Declaring the InventoryService Workflow Configuring Tracking
in the
Web.config
Testing the Workflow Service
Application
618 618 620 622
xxiii
CONTENTS
Loading Tracking Profiles from App.config Implementing the