The workflow runtime and the activity execution model are extremely interesting and deep topics in the world of Windows Workflow Foundation. Considerations had to be made when architecting this framework to ensure that each workflow tenet was appropriately implemented. For example, given the fact that workflow instances persisted to some durable medium at any given time, the state, including each activity’s state, has to be persisted as well. Therefore, every time an activity executes, it does so within a special context that tracks information about the execution of that particular activity instance. This includes property data as well as status information related to the success of the activity’s execution.
The advantage of this type of architecture is that it can act on an activity’s previous execution, even if that activity executed months ago and the workflow instance has been persisted many times between then and now. This allows functionality such as compensation for an activity based on its state at one point in time if an error occurs and some logic needs to roll back previous actions. Imagine a scenario in which an activity initiates an order in a system. Perhaps an issue arises down the road, and the order needs to be canceled. The activity that created the order has access to its prior state, so it is potentially able to make a cancel order call while passing relevant information such as an order number.
The activity execution context comes into play for custom activity developers in two different instances. The first is related to custom composite activities where the parent needs to create new execution contexts for each child. In addition, if a custom activity is being developed that performs iteration, it needs to create a new execution context for the child activities each time they execute. The out-of-the-box While activity follows this rule by spawning a new context for each of its children during every loop iteration.
The goal of this chapter is to help you understand these concepts through further explanation and concrete examples.