pro wf : windows workflow in .net 4 ; [use windows ... · contents runningtheapplication 21...
TRANSCRIPT
ProWF
Windows Workflow in .NET 4
Bruce Bukovics
Apress*
Contents
About the Author xxxi
About the Technical Reviewers xxx»
Acknowledgments •» —• xxx'»
Introduction xxxiv
Chapter 1: A Quick Tour of Windows Workflow Foundation 1
Why Workflow? 1
Workflows Are Different 2
Why Windows Workflow Foundation? 3
Your Development Environment 4
The Workflow Workflow 4
Hello Workflow 5
Creating the Project 6
Declaring the Workflow 8
Adding the Sequence Activity 9
Adding the WriteLine Activity 10
Hosting the Workflow 12
Running the Application 14
Exploring the Xaml 15
Passing Parameters 17
Declaring the Workflow 17
Hosting the Workflow 20
vi
CONTENTS
Running the Application 21
Using Argument Properties 21
Making Decisions 22
Creating the Project 23
Implementing a Custom Activity 23
Defining Arguments 25
Defining Variables 26
Adding the Custom Activity 28
Adding the Switch<T> and Assign Activities 29
Hosting the Workflow 34
Running the Application 35
Debugging the Application 37
Unit Testing 39
Testing the Custom Activity 39
Testing the Workflow 41
Summary 43
Chapter 2: Foundation Overview .45
WF Features and Capabilities 45
Declarative Activity Model 45
Standard Activities 46
Custom Activities 46
Workflow Designer 47
Custom Activity Designers and Validation 47
Multiple Modeling Styles 47
Workflow Debugger 48
Workflow Services 48
Multiple Workflow Hosts 48
Workflow Extensions 49
Persistence 49
vii
CONTENTS
Bookmark Processing 49
Expressions 50
Transaction Support 50
Compensation and Exception Handling 50
Workflow Tracking 50
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.xAssemblies 54
Activity Life Cycle 55
Definition vs. Runtime Instance 55
Definition vs. Runtime Variables 56
Activity States 57
Expressions 57
Visual Basic Expressions 58
VB Primer for Workflow Developers 58
Expression Activities 62
Missing 4 Features 66
State Machine 67
Reuse of WCF Contracts 67
C# Expression Support 68
Tracking to SQLServer 68
Rules Engine 68
Dynamic Updates 68
viii
CONTENTS
Summary 68
CilctpUfSr 3d ACtlWtlfiS iBntaDm nana an «!a * nanBeanRaaBtiaaDBflBBBflHiiBBiiita'Ft
Understanding Activities 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 78
Creating the Project 79
Implementing the Activity 79
Implementing Unit Tests 81
Testing the Activity 83
Declaring an Activity with Xaml 84
Creating the Activity Class 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 89
Implementing Unit Tests 92
Testing the Activity 93
Implementing an Asynchronous Activity 93
Creating the Activity Class 94
ix
CONTENTS
Implementing the Activity 94
Implementing Unit Tests 96
Testing the Activity 97
Using Activities 97
Workflow Building Blocks 97
Activity Data Flow 99
Variable Scoping 100
Standard Activities Summary 101
Standard Activities 103
Control Flow 104
Flowchart 105
Messaging 106
Runtime 107
Primitives 107
Transactions and Compensation 108
Collection Management 109
Error 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 114
Declaring the HostingDemoWorkflow 114
Simple Hosting of the Workflow 117
Passing Arguments with Workflow Properties 118
x
CONTENTS
Declaring a Timeout Value 119
Invoking a Generic Activity,
120
Using the Workflowlnvoker Instance Methods 122
Using the InvokeAsync Method, 122
Using the Beginlnvoke Method 124
Understand the WorkflowApplication Class, 126
Constructing a WorkflowApplication 127
Assigning Code to Delegate Members 127
Managing Extensions 129
Configuring and Managing Persistence 129
Executing a Workflow Instance 130
Managing Bookmarks 131
Manually Controlling a Workflow Instance 132
Using the WorkflowApplication Class 133
Hosting the Workflow with WorkflowApplication 133
Canceling a Workflow Instance 137
Aborting a Workflowlnstance 138
Terminating a Workflowlnstance 139
Using the BeginRun Method 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
Designing the User Interface 154
Implementing the User Interface Code 156
Testing the Application 159
Using the WPF SynchronizationContext 160
Summary 161
Chapter 5: Procedural Flow Control 163
Understanding the Procedural Modeling Style 163
Making Decisions 164
Understanding the If Activity 164
Understanding the Switch<T> Activity 164
Understanding the While and DoWhile Activities 165
Using the While and DoWhile Activities 167
Implementing the InventoryLookup Activity 167
Declaring the Getltemlnventory Workflow 169
Hosting the Workflow 172
Testing the Workflow 173
Using the DoWhile Activity 174
Understanding the Parallel Activity. 176
Understanding Parallel Execution 176
Creating the ParallelDemo Project 179
Declaring the ParallelDemo Workflow 179
Hosting the Workflow 179
Testing the Workflow 180
Adding a Delay Activity 180
Testing the Revised Workflow 181
Using the Parallel Activity 182
Creating the GetltemLocation Project 182
Declaring the GetltemLocation Workflow 182
xii
CONTENTS
Hosting the Workflow 186
Testing the Workflow 187
Obtaining Asynchronous Execution with the Parallel Activity 189
Implementing the InventoryLookupAsync Activity 189
Modifying the GetltemLocation Workflow 191
Testing the Revised Workflow 192
Summary 194
Chapter 6: Collection-Related Activities 195
Understanding the ForEach<T> Activity 195
Understanding the Collection Activities 198
Using the ForEach<T> and Collection Activities 200
Creating the ActivityLibrary Project 200
Implementing Item Structures 200
Implementing the FindlnCollection<T> Activity 201
Declaring the InventoryUpdate Workflow 203
Hosting the Workflow 209
Testing the Workflow 210
Using the ParallelForEach<T> Activity 211
Testing the Revised Workflow 212
Working with Dictionaries 213
Implementing the Dictionary-Related Activities 214
Declaring the InventoryUpdateDictionary Workflow 218
Hosting the Workflow ••220
Testing the Workflow 221
Understanding the InvokeMethod Activity 222
Using the InvokeMethod Activity 223
Revising the Itemlnventory Class 224
Modifying the Workflow 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 Activity 231
FlowSwitch<T> Activity 231
FlowStep Activity 232
Putting It All Together 232
The Flowchart Workflow 233
Making Simple Decisions 233
Implementing the ParseCalculatorArgs Activity 234
Creating the Console Project 235
Defining Arguments and Variables 235
Declaring the Workflow 236
Hosting the Workflow 239
Testing the Workflow 240
Declaring Looping Behavior 241
Implementing the InventoryLookup Activity 241
Creating the Console Project 243
Defining Arguments and Variables 243
Declaring the Workflow 243
Hosting the Workflow 247
Testing the Workflow, 247
Declaring Custom Activities 248
Defining Arguments and Variables 248
Declaring the Activity 249
xiv
CONTENTS
Implementing Unit Tests 251
Testing the Activity 253
Mixing the Two Styles 254
Implementing Item Structures 254
Implementing the FindlnCollection<T> Activity 255
Creating the Console Project 256
Defining Arguments and Variables 256
Declaring the Workflow 257
Hosting the Workflow 261
Testing the Workflow 262
Summary 263
Chapter 8: Host Communication.... 265
The Need for Long-Running Workflows 265
Understanding Bookmarks 266
Using Bookmarks 268
Implementing the GetString Activity 268
Implementing the ParseCalculatorArgs Activity 269
Creating the Console Project 270
Hosting the Workflow 273
Testing the Workflow 275
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 an Alternate Extension 286
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
Testing the Workflow 300
Using the InvokeAction Activity 300
Understanding the Pick Activity 302
Using the Pick Activity 303
Implementing the WaitForBookmark Activity 303
Creating the Console Project 304
Defining Variables 304
Declaring the ProblemReporting Workflow 304
Hosting the Workflow 309
Testing the Workflow 311
Summary 312
Chapter 9: Workflow Services ..313
Introducing Workflow Services 313
Understanding WCF 314
Defining Service Contracts 315
Configuring Endpoints and Bindings 316
Hosting and Configuration 316
xvi
CONTENTS
Understanding Workflow Services, 317
Messaging Activities 317
Service Contracts and Message Types 322
Correlation 324
Declaration and Hosting Options 326
Controlling Workflow Service Instances 327
Declaring a Workflow Service,
327
Tasks for a Request/Response Operation 327
Implementing the OrderProcessing Workflow Service 328
Creating the ServiceLibrary Project 329
Implementing Request and Response Classes 329
Declaring the Service Operation 332
Populating the Response 337
Configuring the Service 339
Testing the Service 339
Publishing a Workflow Service to IIS 341
Enhancing the Web.config 341
Publishing to IIS 342
Implementing a Client Application 342
Adding a Service Reference 343
Invoking the Service 343
Reviewing the Configuration 346
Testing the Client Application 347
Implementing a Workflow Client 348
Implementing Custom Activities 349
Adding a Service Reference 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 a Service 362
Implementing the Sen/iceHost Application 363
Configuring the Service Host 366
Testing the Self-hosted Service 367
Using the Workflow/Client Application 367
Summary 368
Chapter 10: Workflow Services Advanced Topics 369
Using Context-Based Correlation 369
Guidelines for Context-Based Correlation 370
Declaring the ShipOrder Workflow Service , 371
Modifying the OrderProcessing Service 379
Hosting the ShipOrder Workflow Service 384
Configuring the ServiceHost Application 384
Testing the Revised OrderProcessing Workflow 385
Using Content-Based Correlation 386
Guidelines for Content-Based Correlation 386
Modifying the ShipOrder Workflow Service 386
Configuring the ServiceHost Application 388
Testing the Revised Workflow Service 388
Implementing a Duplex Message Exchange Pattern 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
xviii
CONTENTS
Using a Custom Workflow Extension 405
Implementing the OrderUtilityExtension 406
Implementing the GetOrderld Activity 406
Modifying the OrderProcessing Workflow Service 407
Adding 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 Extension 426
Implementing Activities that use the Extension, 428
Implementing Bookmark-Related Activities 429
Declaring the OrderEntry Workflow 431
Hosting and Persisting the Workflow 435
xix
CONTENTS
Configuring the Application 440
Testing the Application 441
Understanding WorkflowServiceHost Persistence 443
Using the SqIWorkflowlnstanceStore with WorkflowServiceHost 444
Declaring the OrderEntryService Workflow 445
Hosting the Workflow Service 452
Testing the ServiceHost Project 457
Implementing a Client Project 458
Configuring the Client Project 464
Testing the Client Project 465
Summary 467
Chapter 12: Customizing Workflow Persistence 469
Understanding the PersistenceParticipant Classes 469
The PersistenceParticipant Class 470
The PersistencelOParticipant Class 471
Which Class to Use? 471
Using the PersistenceParticipant Class 472
Modifying the ItemSupportExtension Class 472
Testing the Revised Extension 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 480
xx
CONTENTS
Modifying the Client Application 480
Configuring the Client Application 482
Testing the Revised Example 482
Implementing a Custom Instance Store 483
Understanding the InstanceStore Class 484
Understanding the Instance Persistence Commands 484
Understanding the InstancePersistenceContext Class 485
Implementing a File System-Based Instance Store 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 the Example Workflow 508
Enabling LINQ Access to the AventureWorks Database 510
Implementing the GetOrderDetail Activity 511
Implementing the UpdateProductlnventory Activity 514
Implementing the InsertTranHistory Activity 516
Implementing the External Update Activity 517
Implementing the DisplayProductlnventory Activity 518
Declaring the Updatelnventory Workflow 520
Declaring the Displaylnventory Workflow 523
Hosting the Workflow 526
Testing 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 542
Hosting the Workflow 545
Testing the Workflow 545
Using a Host Transaction 546
Hosting the Workflow 547
Testing the Workflow 548
Understanding Compensation 549
Using the CompensableActivity 551
Implementing the ExternalVoid Activity 551
Declaring the UpdatelnventoryComp Workflow 552
Hosting the Workflow 555
Testing the Workflow 555
Manually Triggering Compensation 557
Declaring the UpdatelnventoryManualComp Workflow 557
Hosting the Workflow 562
Testing the Workflow 562
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
xxii
CONTENTS
Tracking Records 568
Tracking Profiles 574
Tracking Participants 578
Using ETW Workflow Tracking 579
Providing AdventureWorks Access 579
Copying the Custom Activities 580
Declaring the Workflow 580
Hosting the Workflow 584
Enabling ETW Workflow Tracking 584
Testing the Workflow 586
Viewing the Tracking Data 586
Using Tracking Profiles 591
Including Selected Workflow Instance States 592
Including All Workflow Instance States 593
Adding Selected Activity States 594
Targeting Selected Activities 596
Adding Selected Scheduled Records 598
Including Custom Tracking Records 600
Developing a Custom Tracking Participant 604
Implementing the Tracking Record Serializer 605
Implementing the Custom Tracking Participant 608
Testing the Tracking Participant 611
Developing a Nonpersisting Tracking Participant 615
Implementing the Tracking Participant 615
Testing the Tracking Participant 615
Using Workflow Tracking with a Declarative Service Application 618
Declaring the InventoryService Workflow 618
Configuring Tracking in the Web.config 620
Testing the Workflow Service 622
xxiii
CONTENTS
Loading Tracking Profiles from App.config 623
Implementing a Tracking Profile Loader 623
Defining the Tracking Profile in the App.config file 625
Testing the Tracking Profile Loader 626
Summary 626
Chapter 15: Enhancing the Design Experience -629
Understanding Activity Designers 629
ActivityDesigner 630
Modelltem 630
ExpressionTextBox 631
ArgumentToExpressionConverter 631
Understanding Expression Types 632
WorkflowltemPreseriter and WorkflowltemsPresenter 632
Metadata Store and Designer Assignment 633
The Custom Designer Workflow 634
Supporting Activity Properties 634
Creating the Projects 634
Implementing the CalcShipping Activity 635
Viewing the Default Design Experience 636
Declaring a Custom Designer 636
Associating the Activity with the Designer 639
Using the MetadataStore to Associate a Designer 640
Adding an Icon 643
Supporting Expanded and Collapsed Modes 645
Declaring the Collapsible Designer 645
Changing the Designer Attribute 648
Testing the Collapsible Designer 648
xxiv
CONTENTS
Supporting a Single Child Activity 649
Implementing the MyWhile Activity 650
Declaring a Custom Designer 650
Testing the Designer 652
Supporting Multiple Child Activities 653
Implementing the MySequence Activity 653
Declaring a Custom Designer 654
Testing the Designer 655
Supporting the ActivityAction Activity 657
Implementing the MyActivityWithAction Activity ,657
Declaring a Custom Designer 659
Testing the Designer 660
Understanding Validation 661
Validation Attributes 662
Validation Code 662
Constraints 662
Using Validation Attributes 663
Using the RequiredArgument Attribute 663
Using the OverloadGroup Attribute 664
Adding Validation in Code 667
Adding an Error 667
Adding a Warning 669
Using Constraints for Validation 669
Implementing a Simple Constraint 670
Validating Against Other Activities 672
Manually Executing Validation ,679
Implementing the Validation Tests 679
Executing the Validation Tests 682
XXV
CONTENTS
Implementing Activity Templates 683
Implementing the Template 684
Testing the Template 685
Summary 685
Chapter 16: Advanced Custom Activities 687
Understanding Your Parental Responsibilities 687
Configuring Activity Metadata 688
Scheduling Child Execution 690
Handling Child Completion 691
Handling Bookmarks 692
Handling a Cancellation Request 693
Reacting to Abort and Terminate 693
Scheduling a Single Child 693
Implementing the Custom Activity 694
Implementing the Activity Designer 696
Declaring a Test Workflow 697
Implementing a Test Application 699
Testing the Activity 701
Repeating Execution of a Single Child 702
Implementing the Custom Activity 703
Implementing the Activity Designer 705
Declaring a Test Workflow 706
Testing the Activity 707
Handling Exceptions 709
Throwing an Exception 709
Handling the Exception 712
Scheduling Multiple Children 714
Implementing the Custom Activity 714
xxvi
CONTENTS
Implementing the Activity Designer 717
Declaring a Test Workflow 719
Testing the Activity 721
Testing the Condition Logic ,
723
Scheduling Parallel Execution 725
Implementing the Custom Activity ,725
Declaring a Test Workflow 728
Testing the Activity ,731
Scheduling an ActivityAction 734
Implementing the Custom Activity 734
Implementing the Activity Designer 737
Declaring a Test Workflow 738
Testing the Activity 739
Using the DynamicActivity Class ..741
The Example Scenario 741
Constructing a DynamicActivity 742
Testing the Activity 746
Using Execution Properties 747
Implementing the OrderScope Activity 748
Implementing the QrderAddltems Activity 749
Declaring a Test Workflow 750
Testing the Activities 751
Summary 752
Chapter 17: Hosting the Workflow Designer.... 753
Understanding the Workflow Designer Components 753
Understanding the WorkflowDesigner Class 754
Understanding the ToolboxControl 755
Defining New Activities 756
xxvii
CONTENTS
Understanding the EditingContext 757
Providing Designer Metadata 760
The Self-hosting Designer Workflow 760
Implementing a Simple Workflow Designer 760
Creating the Application 761
Declaring the Window Layout 761
Implementing the Application 763
Testing the Application 769
Executing the Workflow 772
Modifying the Application 772
Testing the Application 773
Loading and Saving the Definition 775
Modifying the Application 775
Testing the Application 777
Displaying Validation Errors 777
Implementing the ValidationErrorService 777
Modifying the Application 778
Testing the Application 779
Adding Activities to the Toolbox 780
Modifying the Application 781
Testing the Application 785
Providing Designer Metadata 788
Referencing the Custom Designer 788
Modifying the Application 789
Testing the Application 789
Tracking the Selected Activity 790
Modifying the Application 791
Testing the Application 792
xxviii
CONTENTS
Modifying the Context Menu 793
Modifying the Application 794
Testing the Application 796
Locating the Arguments 796
Modifying the Application 797
Testing the Application 798
Summary 799
Chapter 18: WF 3.x Interop and Migration 801
Reviewing Migration Strategies 801
Continuing with WF 3.x 802
Migrating to WF4 804
Preparing for Migration 804
Understanding the Interop Activity 806
Limitations of the Interop Activity 807
Invoking a WF 3.x Activity 807
Implementing a WF 3.5 Activity 808
Declaring a Test Workflow 811
Testing the Workflow 814
Invoking a WF 3.x Workflow 815
Implementing a WF 3.5 Custom Activity 815
Implementing the WF 3.5 Workflow 817
Declaring a Test Workflow 822
Testing the Workflow 823
Using the ExternalDataExchangeService 823
Implementing the Event Arguments 824
Implementing the Data Exchange Service 825
Generating the Communication Activities 826
Declaring the WF 3.5 Workflow 826
xxix
CONTENTS
Declaring a Test Workflow 830
Testing the Workflow 831
Executing Rules Using the Interop Activity 834
Implementing the Salesltem Class 834
Declaring the WF 3.5 Workflow and Rules 835
Declaring a Test Workflow 838
Testing the Workflow 838
Executing Rules Using a Custom Activity 842
Implementing a SalesltemWrapper 842
Implementing the ApplyRules Activity 843
Declaring a Test Workflow 845
Testing the Workflow 847
Summary 847
Appendix An Glossary .......... > » ............. >.••>343
Appendix B: Comparing WF 3.x to WF 4 .861
WF 3.xto WF 4 Architectural Differences 861
WF3.A-IO WF 4 Activities 865
IllCf®X BianaaiimitaiaiiiiiiKiiaaii'iiiBiiiaiii iitaaagmi»iinn>i iitiiBiDggggiiBniaiatiiiaiaaiiiiiBiiiBiBfliggiiDnBnniBiii
XXX