learnWorkflow: introductions




chapter introductions

What follows are the introduction sections from each chapter of the book. The introduction provides a brief overview of what you'll learn from the chapter.

chapter 1: a quick tour of windows Workflow foundation

This chapter introduces you to Windows Workflow Foundation (WF). Instead of diving deeply into any single workflow topic, it provides you with a brief sampling of topics that are fully presented in other chapters.

You’ll learn why workflows are important and why you might want to develop applications using them. You’ll then jump right in and implement your very first functioning workflow. Additional hands on examples are presented that demonstrate other features of Windows Workflow Foundation.

chapter 2: foundation overview

The purpose of this chapter is to provide a grand tour of Windows Workflow Foundation (WF). It includes a high-level description of the major features of WF. The chapter also provides some necessary background information that will help you get the most out of the remaining chapters. Because of the summary focus of this chapter, it doesn’t contain much actual workflow code.

The chapter begins with a brief description of the features and capabilities that are included with WF. Following this, I provide an overview of the .NET assemblies and namespaces within them that are related to WF. Topics related to the activity life cycle are then discussed, followed by a short primer on workflow expressions. The chapter concludes with a list of the top features that are currently missing from WF 4.

chapter 3: activities

The focus of this chapter is the primary building block of all workflows: the activity. Activities are where you place the business logic that is specific to your particular problem domain. In this chapter, you will learn how to develop your own custom activities using the base classes that ship with Windows Workflow Foundation (WF). You can choose to create new activities in code completely from scratch, or you can declaratively assemble existing activities into a new custom activity. This chapter demonstrates both authoring styles.

This chapter also provides a high-level review of the standard activities that are provided with WF. You will become acquainted with the available activities, but you will not learn how to use them in detail. This is the one chapter that provides an overview of all the available activities. Many of the individual activities are difficult to understand unless they are discussed as part of a larger subject area. For this reason, the subsequent chapters in this book each focus on a specific subject area and provide additional detail on the activities that are relevant to that subject.

chapter 4: workflow hosting

This chapter focuses on hosting and executing workflows. Regardless of the type of host application, the WorkflowInvoker and WorkflowApplication classes are used to execute your workflows. The chapter describes how to use each class and then demonstrates features of each class with a number of short examples.

The WorkflowInvoker and WorkflowApplication classes require that you create an instance of the workflow type that you want to execute. Windows Workflow Foundation (WF) also provides the ability to load and execute a workflow directly from a Xaml document. The ActivityXamlServices class provides this functionality and is described and demonstrated in this chapter.

Invoking a workflow from an ASP.NET web application is a common scenario that is demonstrated later in the chapter. Finally, the chapter concludes with an example that demonstrates how to execute and manage multiple instances of a workflow from a Windows Presentation Foundation (WPF) application.

chapter 5: procedural flow control

The focus of this chapter is a core set of procedural flow control activities that are included with Windows Workflow Foundation (WF). These are some of the most frequently used activities that implement branching and looping. The If and Switch<T> activities are first discussed, followed by examples that demonstrate the While and DoWhile activities. The Parallel activity is then discussed, followed by several examples that demonstrate its use.

chapter 6: collection-related activities

This chapter focuses on the activities that enable you to work with collections of data. Windows Workflow Foundation (WF) includes a ForEach<T> activity that iterates over each element in a collection, executing the same activity for each element. WF also includes a parallel version of this activity and a set of activities that allow you to manipulate collections, adding and removing elements and so on. After a brief summary of these activities, this chapter presents an example that demonstrates their use.

The standard activities provided with WF are designed to work with simple collections, not dictionaries. To demonstrate one way to work with dictionaries, this chapter presents a set of custom activities. It then presents the original example workflow again using a dictionary instead of a collection.

Finally, the chapter ends with a discussion and demonstration of the InvokeMethod activity. This activity allows you to declaratively invoke an instance or static method.

chapter 7: flowchart modeling style

The focus of this chapter is the flowchart modeling style. This style of modeling workflows enables you to use direct links between activities to control the flow of execution. In this chapter, I first review the activities that are provided with Windows Workflow Foundation (WF) to support this modeling style. After explaining how to model a workflow using this style, I revisit several examples that were presented in earlier chapters. This is done to contrast how the two modeling styles (procedural and flowchart) can be used to solve similar business problems.

chapter 8: host communication

The focus of this chapter is direct communication between the host application and a workflow instance. The chapter begins with an overview of long-running workflows and the bookmark mechanism used to implement them. Bookmarks provide a way to temporarily suspend execution of a workflow instance while it is waiting for external input. They also provide a way to pass data to a waiting instance as the bookmark is resumed.

But bookmarks only permit data to be passed into a workflow instance, not in the other direction to the host application. The use of workflow extensions for sending data to a host application is discussed and demonstrated next.

The ActivityAction and ActivityFunc activities are a general-purpose extensibility mechanism. They allow you to execute activities that are provided outside the workflow declaration. The chapter continues with a discussion and demonstration of using an ActivityAction as a way to communicate between a workflow instance and the host application.

The chapter ends with coverage of the Pick and PickBranch activities. These activities allow you to model workflows that can create and wait for multiple bookmarks at the same time.

chapter 9: workflow services

This chapter focuses on theWindows Communication Foundation (WCF) support that is provided by Windows Workflow Foundation (WF). This broad category of WCF support falls under the moniker of workflow services. Included with this support is the ability to declaratively author WCF services using WF as well as to invoke WCF services from within a workflow.

The chapter begins with a brief introduction of workflow services and WCF followed by an in-depth discussion of the WF classes and activities that you will use to declare and invoke WCF services. The centerpiece example of this chapter is an order processing service that simulates an ecommerce transaction. The example begins with a relatively simple request/response service operation. This first basic example is used to demonstrate how to initially test the service, how to publish the service to Internet Information Services (IIS), how to develop a self-hosting application for the service, and how to implement nonworkflow and workflow client applications that invoke the service.

In Chapter 10, you will expand on this basic example by implementing additional workflow services that are consumed by this original workflow.

chapter 10: workflow services advanced topics

This chapter continues coverage of the Windows Communication Foundation (WCF) support that is provided by Windows Workflow Foundation (WF). Chapter 9 began the discussion with an introduction to workflow services and WCF in general. In that chapter, you declared a simple request/response service operation, hosted it using Internet Information Services (IIS), developed a self-hosting application, and developed two client applications to test it.

The chapter expands on this basic example from Chapter 9 by implementing additional workflow services that are consumed by the original workflow. One additional service demonstrates how to use context-based and content-based correlation to route messages to the correct service instance. Another example service uses the duplex message exchange pattern to invoke callback operations of the calling service. The final example demonstrates how to use workflow extensions with services.

The chapter concludes with a discussion of exception and fault processing, flowing transactions into a workflow service, and the use of standard WF behaviors to fine-tune workflow service performance.

chapter 11: workflow persistence

One of the most important capabilities of Windows Workflow Foundation (WF) is the ability to persist workflow instances (save and reload them at a later time). Without persistence, the lifetime of your workflows is limited. When the host application is shut down, any workflow instances simply cease to exist. Without workflow persistence, the development of long-running workflow applications would be difficult, if not completely impossible.

The chapter begins with an overview of the workflow persistence support in WF. The discussion includes information on how to enable persistence when you are using the WorkflowApplication class to host your workflows. Other sections describe how persistence is used with workflow services that are hosted by the WorkflowServiceHost class. Coverage of the SqlWorkflowInstanceStore, which supports persistence to a SQL Server database, is also included.

The chapter includes hands-on examples that demonstrate persistence with WorkflowApplication and WorkflowServiceHost.

chapter 12: customizing workflow persistence

This chapter continues the discussion of workflow persistence that began in Chapter 11. In that chapter, you learned the basics of workflow persistence using the SqlWorkflowInstanceStore. By following the examples presented in that chapter, you should now know how to enable persistence for applications that are hosted by the WorkflowApplication class as well as workflow services hosted by IIS or self-hosted by the WorkflowServiceHost class.

This chapter focuses on ways to extend or customize workflow persistence. It builds upon the examples that were presented in Chapter 11. Additional examples in this chapter extend persistence using the PersistenceParticipant class and demonstrate the promotion of properties to make them externally queryable. Another example demonstrates how to use the WorkflowControlEndpoint to manage active workflow instances.

The chapter concludes with an example that implements a custom instance store. The instance store persists workflow instances to the file system rather than to a database.

chapter 13: transactions, compensation, and exception handling

This chapter focuses on the mechanisms provided by Windows Workflow Foundation (WF) to support the handling of exceptions and to ensure the consistency of work that is performed within a workflow.

The chapter begins with an overview of the default exception handling logic. Following that introduction, a preliminary example is developed that is used to illustrate the concepts throughout the chapter. Enhancements to the example workflow include using the TryCatch activity to handle exceptions, using the TransactionScope activity to provide transaction support, and using the CompensableActivity to execute compensation logic.

chapter 14: workflow tracking

Workflow tracking is a built-in mechanism that automatically instruments your workflows. By simply adding a tracking participant to the workflow runtime, you are able to track and record status and event data related to each workflow and each activity within a workflow.

The chapter begins with an overview of the workflow tracking functionality included with Windows Workflow Foundation (WF). Following the overview, a more detailed discussion of tracking records, profiles, and participants is presented.

The chapter contains numerous examples that demonstrate how to use the ETW (Event Tracing for Windows) tracking participant that is included with WF. A series of examples is presented that demonstrate how tracking profiles can be used to filter the type of tracking data that is passed to a tracking participant. The creation of custom tracking records is also demonstrated.

The ability to develop your own tracking participant is important since it allows you to directly consume the tracking records in any way that is needed by your application. Two different custom tracking participants are implemented in this chapter.

The chapter concludes with a demonstration of how to configure workflow tracking for declarative workflow services and how to load the tracking configuration from an App.config file.

chapter 15: enhancing the design experience

One of the responsibilities of an activity is to cooperate with the workflow designer in order to provide an appealing and productive design experience. This chapter focuses on the mechanisms provided with Windows Workflow Foundation (WF) that make the design-time experience easier to use and less error prone. Rather than focus on a particular runtime feature of WF, this chapter is all about helping the developers who use your custom activities at design time.

This chapter begins with an overview of the classes that you will use to create custom activity designers. Several designer scenarios are then presented in a series of examples. The first example exposes the properties of an activity directly on the design surface. Subsequent examples demonstrate custom designers that allow you to add one or multiple children to an activity. Another designer targets activities that support the ActivityAction class as a property.

Custom activity designers are really only half of the design-time story. The second half is activity validation. WF provides several ways that you can enforce validation logic for your activities. In a series of examples, validation attributes, code, and constraints are demonstrated. WF also allows you to manually perform activity validation outside the design experience. This is demonstrated in another example.

The chapter concludes with an example that creates an activity template. Activity templates allow you to create combinations of activities or preconfigured activities that are added to the design surface just like a normal activity.

chapter 16: advanced custom activities

This chapter focuses on several advanced custom activity scenarios. Most of these scenarios are related to the execution of one or more children. The chapter begins with a general overview of the process that you must follow to schedule the execution of children. Your responsibilities as a parent activity are also reviewed. A simple activity that schedules a single child for execution is then demonstrated.

Following this first example, the chapter presents an activity that repeats execution of a single child while a condition is true. This example also explores the options that are available to you for handling an exception that is thrown by a child activity.

Other examples in this chapter demonstrate how to execute multiple children sequentially, or using a parallel execution pattern. An activity that supports an ActivityAction is also demonstrated. Dynamically constructing an activity using the DynamicActivity class is demonstrated next.

The chapter concludes with an example that demonstrates the use of execution properties and bookmark options.

chapter 17: hosting the workflow designer

Most of the time you will author new activities and workflows using the workflow designer that is integrated into Visual Studio. However, you may need to enable end users of your application to also design activities and workflows. You can allow them to load and customize existing Xaml files or to create new ones.

The workflow designer is not limited to use only within the Visual Studio environment. WF provides the classes necessary to host this same designer within your applications. This chapter is all about hosting this designer. After a brief overview of the major workflow designer components, you will implement a simple application that hosts the workflow designer. In subsequent sections, you will build upon the application, adding new functionality with each section.

chapter 18: wf 3.x interop and migration

This chapter focuses on strategies for dealing with existing WF 3.0 or 3.5 applications (WF 3.x). The chapter begins with an overview of the migration strategies that are available to you. Following this, the Interop activity is discussed and demonstrated. This activity enables you to execute some WF 3.x activities within the WF 4 runtime environment.

Several specific migration scenarios are demonstrated next. The use of the WF 3.x ExternalDataExchangeService with WF 4 workflows is demonstrated followed by two examples that use the WF 3.x rules engine.

appendix a: glossary

This is a glossary of commonly used WF terms.

appendix b: comparing wf 3.x to wf 4

This appendix contains a table that summarizes the major architectural differences between WF 3.x and WF 4.

Also included is a table that compares the list of WF 3.x and WF 4 standard activities. In cases where there isn’t an exact match between WF 3.x and WF 4, I list the closest match that could be used to provide similar functionality or solve a similar problem.