www.it-ebooks.info Beginning WF Windows Workflow in .NET 4.0 n n n Mark J. Collins www.it-ebooks.info Beginning WF: Windows Workflow in .NET 4.0 Copyright © 2010 by Mark J. Collins All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-2485-3 ISBN-13 (electronic): 978-1-4302-2486-0 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. President and Publisher: Paul Manning Lead Editor: Jonathan Hassell Technical Reviewer: Michael Mayberry Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Project Manager: Debra Kelly Copy Editor: Nancy Sixsmith Compositor: Laureltech Indexer: BIM Indexing & Proofreading Services Artist: April Milne Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. For information on translations, please e-mail rights@apress.com, or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at www.apress.com. www.it-ebooks.info Dedicated to my wife, Donna. Your worth is far above rubies (Prov 31:10). I love you! www.it-ebooks.info v Contents at a Glance Contents vii About the Author xix About the Technical Reviewer xx Acknowledgments xxi Introduction xxii n Part 1: Introduction 1 n Chapter 1: Building a Simple Workflow 3 n Chapter 2: Coded Workflows 23 n Chapter 3: Flowchart Workflow 33 n Part 2: Designing Workflows 43 n Chapter 4: Passing Arguments 45 n Chapter 5: Replicated Activities 59 n Chapter 6: Exception Handling 69 n Chapter 7: Extending the Built-In Activities 79 n Part 3: Communication 93 n Chapter 8: Send and Receive 95 n Chapter 9: Communicating with the Host Application 123 n Chapter 10: Web Services 151 www.it-ebooks.info n CONTENTS AT A GLANCE vi n Part 4: Workflow Extensions 175 n Chapter 11: SQL Persistence 177 n Chapter 12: Custom Extensions 209 n Chapter 13: Tracking 229 n Chapter 14: Transactions 255 n Chapter 15: Transactions with Persistence 277 n Chapter 16: WorkflowServiceHost 289 n Part 5: Advanced Topics 317 n Chapter 17: Compensation, Confirmation, and Cancellation 319 n Chapter 18: Collections 345 n Chapter 19: Interoperability with Workflow 3.5 361 n Chapter 20: Policy 375 n Appendix: Sample Workflow Project 405 n Index: 439 www.it-ebooks.info vii Contents About the Author xix About the Technical Reviewer xx Acknowledgments xxi Introduction xxii n Part 1: Introduction 1 n Chapter 1: Building a Simple Workflow 3 A Simple Workflow 4 Exploring the IDE 4 Designing the Workflow 5 Reviewing Program.cs 6 Running the Application 7 Adding Procedural Elements 7 Using Variables 8 If 10 Assign 11 While 12 Sequence 13 Delay 13 More Embellishments 14 Running the Application 15 www.it-ebooks.info n CONTENTS viii Navigating the Designer 15 Looking a Bit Deeper 17 Differences from Previous Versions 21 n Chapter 2: Coded Workflows 23 Creating a Console Application 23 Defining the Workflow 24 Implementing Level 1 25 Implementing Level 2 26 Implementing Level 3 28 Running the Application 29 Review 32 n Chapter 3: Flowchart Workflow 33 Creating a Flowchart Workflow 33 Designing the Flowchart 34 Defining Connections 34 FlowDecision 35 Running the Application 37 Flow Switch 38 Adding a FlowSwitch Activity 38 Adding the FlowStep Activities 39 Running the Application 40 Parallel 40 Adding a Parallel Activity 40 Adding the Branches 41 Running the Application 42 n Part 2: Designing Workflows 43 n Chapter 4: Passing Arguments 45 Creating a New Solution 45 www.it-ebooks.info n CONTENTS ix Defining the Order Class 46 Implementing the Workflow 48 Defining the Arguments 49 Designing the Workflow 51 Switch Activity 51 Expression Activities 53 Invoking the Workflow 56 Running the Application 58 n Chapter 5: Replicated Activities 59 Reusing the Chapter 4 Project 59 Adding OrderItem Processing 61 ForEach Activity 61 Adding Order Items 65 Running the Application 66 ParallelForEach Activity 68 n Chapter 6: Exception Handling 69 Reusing the Chapter 5 Project 69 Adding the Check Stock Activity 70 TryCatch Activity 70 Defining an Exception 70 ForEach Activity 72 If Activity 72 Throw Activity 73 Catch Activity 74 Running the Application 76 Exceptions 77 n Chapter 7: Extending the Built-In Activities 79 Reusing the Chapter 6 Project 79 www.it-ebooks.info n CONTENTS x Using Custom Activities 80 Implementing a Custom Activity 80 Using the LookupItem Activity 82 Running the Application 85 InvokeMethod Activity 86 Creating a Discount Class 86 Using the InvokeMethod Activity 87 Adding the Discount 91 Running the Application 91 Summary 92 n Part 3: Communication 93 n Chapter 8: Send and Receive 95 Creating the Project 95 Define the Messages 96 Application Configuration 101 Defining the Workflows 102 Client–SendRequest 102 Server–ProcessRequest 107 Implementing the Application 114 WorkflowServiceHost 114 WorkflowInvoker 116 Running the Application 118 Configuring a Library Branch 118 Expected Results 120 n Chapter 9: Communicating with the Host Application 123 Creating a WPF Project 123 Reusing the Classes from Chapter 8 124 Defining the Window Form 125 www.it-ebooks.info [...]... launch the workflow The default implementation generated by the template is shown in Listing 1-1 Listing 1-1 Default Program.cs Implementation using System; 6 www.it-ebooks.info CHAPTER 1 ■ BUILDING A SIMPLE WORKFLOW using System.Linq; using System.Activities; using System.Activities.Statements; namespace Chapter01 { class Program { static void Main(string[] args) { WorkflowInvoker.Invoke(new Workflow1 ());... .345 Creating a Collection 345 Defining the Shopping List 346 Initial Workflow 346 AddToCollection Activity 348 Invoking a Workflow 348 Running the Application 349 Printing and Sorting .349 Printing the Collection 349 Sorting the Collection 351 xvi www.it-ebooks.info n CONTENTS... similar to variables, but they are intended for passing data in or out of the workflow You can think of them as method parameters Figure 1-4 shows what the Arguments window looks like Notice the Direction column; it defines whether the data is passed in to the workflow or sent out of the workflow Figure 1-4 Viewing workflow arguments Designing the Workflow The initial workflow designer is empty You will... not described in a step-by-step fashion Instead, the final code can be downloaded from www.apress.com xxiv www.it-ebooks.info PART 1 ■■■ Introduction The Workflow Foundation included in Net 4.0 (referred to as WF 4.0) represents a whole new paradigm for building workflow- based applications It has been completely reengineered from the ground up In this section, you’ll design some simple workflows and... 380 Creating a Rule Set 383 Defining the Rules 384 Understanding Rule Sets 387 Determining the Priority 390 Entering the Priority Rules 391 Creating a Workflow Application 392 Creating a Custom Activity 393 Incrementing the Activity Counters 396 Creating the Main Workflow ... www.it-ebooks.info n CONTENTS n Part 4: Workflow Extensions 175 n Chapter 11: SQL Persistence 177 Creating the Application 177 Renaming the Window 178 Defining the Window Form 178 Implementing a TextWriter 181 Setting Up the Database 185 Creating a Database 185 Installing the Schema 185 Creating... 230 Overriding the Track() Method 232 Configuring a Tracking Participant 233 Configuring a Tracking Profile 234 CustomTrackingRecord 236 Running the Application 238 Event Tracing for Windows (ETW) 238 Setting Up the Extension 238 Configuring the TrackingProfile 239 Running the Application... www.it-ebooks.info About the Author n Mark Collins wrote his first software program using Basic on the TRS-80 in 1978 As technology has evolved, so has his interest and enjoyment of this wonderful world of software Mark’s career has included many varied opportunities, including being an electrical engineer for IBM, being a system acquisition officer for the U.S Air Force, spending 12 years designing and building... exceptions In Chapter 7, you’ll explore the two main ways to extend the workflow activities: creating a custom activity and executing the InvokeMethod activity Section 3: Communication In the third section, you’ll build workflows that take advantage of the integration with the Windows Communication Foundation (WCF) The project in Chapter 8 builds a console application that communicates with other instances... same application using WCF messages In Chapter 9, the console app is replaced with a Windows Presentation Foundation (WPF) application, which demonstrates how the application and workflow can interact with each other In Chapter 10, you’ll host a workflow in a WCF web service You’ll also consume that service using a workflow application Section 4: Workflow Extensions A key component of workflow design . www.it-ebooks.info Beginning WF Windows Workflow in .NET 4. 0 n n n Mark J. Collins www.it-ebooks.info Beginning WF: Windows Workflow. Collections 345 Creating a Collection 345 Defining the Shopping List 346 Initial Workflow 346 AddToCollection Activity 348 Invoking a Workflow 348 Running