Task 4: Define the WaitingForOrder State
In this task, you define the first state in the OrderProcessingWorkflow
(the WaitingForOrder
state) by adding several child activities.
This state is the initial state in the state machine workflow. It is an event-driven state. Therefore, it waits until the NewOrder
event, which is declared and defined in the previous two tasks, is raised from the Simple Order Form host application. When the NewOrder
event is raised, the OrderProcessingWorkflow
sends a status message back to the host application using the local service interface created in task 2. This indicates that the order was received.
The last step in this state activity is to transition to the next stage in the state machine by using a SetStateActivity.
Note
Although you are encouraged to follow the exercises in a linear manner, it is not required. You can start this exercise by opening the sample project and proceeding to the steps in the following sections.
Creating the Activities in the State
To create new WaitingForOrder activities and variables
In the
OrderProcessingWorkflow
workflow designer, create the activities and variables in the following table.To create the activities, drag the appropriate activity from the Windows Workflow v3.0 section of the toolbox to the appropriate location in the designer surface, then use the Properties window to set the properties on the activity. Be sure to add activities within a parent activity in the order listed here, so that the child activities execute in the correct order.
To create the variables, open the code view for the workflow and add the members as fields on the workflow itself. Declare the fields as Private.
The EventDrivenActivity serves as the container for the other three activities in the state (the first activity in a state activity is required to be an EventDrivenActivity.) Once you have placed the EventDrivenActivity into the
WaitingForOrderStateActivity
, double-click it to open it so that child activities can be placed within. The HandleExternalEventActivity serves as the event handler for the incoming event. The CallExternalMethodActivity then updates the UI after the event is received, using the interface created in Task 2. When the state’s logic is complete, the SetStateActivity activity transfers workflow execution to the next state. TheNewOrderEventArgs
object is used to receive the data passed into the workflow. The remaining fields are used to store the data passed into the workflow.Type Name Location (Parent) Properties EventDrivenActivity
eventDriven1
WaitingForOrderState
HandleExternalEventActivity
newOrderExternalEvent
eventDriven1
(Double-click theeventDriven1
activity to open it.)- InterfaceType: IOrderingService
- EventName: NewOrder
CallExternalMethodActivity
updatestatusOrderReceived
eventDriven1
(Double-click theeventDriven1
activity to open it.)- InterfaceType: IOrderingService
- Method Name: ItemStatusUpdate
SetStateActivity
setStateActivityOrderProcessing
eventDriven1
(Double-click theeventDriven1
activity to open it.)- TargetStateName: OrderProcessingStateActivity
NewOrderEventArgs
receivedOrderDetails
OrderProcessingWorkflow (field)
orderId
OrderProcessingWorkflow (field)
orderItem
OrderProcessingWorkflow (field)
orderQuantity
OrderProcessingWorkflow (field)
String
orderIemStatus
OrderProcessingWorkflow (field)
When the activities are defined, the workflow designer should look like the following illustration.
Expanding the
eventDriven1
activity shows the following:Next, create properties for accessing the fields. Create a Guid property named
Id
and define aGET
method that returns the value of theorderId
field.public Guid Id { get { return this.orderId; } }
Create a String property named
ItemStatus
and define aget
method that returns the value of theorderItemStatus
field.public string ItemStatus { get { return this.orderItemStatus; } }
Create a String property named
Item
and define aget
method that returns the value of theorderItem
field.public string Item { get { return this.orderItem; } }
Creating the OrderReceived Method
To create the OrderReceived method
Create the handler that executes when the order is received by creating a new private method named
OrderReceived
that accepts a Object method namedsender
and anEventArgs
object namede
. These are the two standard parameters for event handlers.private void OrderReceived(object sender, EventArgs e)
When this event fires, the incoming information from the event is stored in the variables that were previously created. In the
OrderReceived
method, set theorderId
field equal to theItemId
property of thereceivedOrderDetails
object.Set the
orderItem
field equal to theItem
property of thereceivedOrderDetails
object.Set the
orderQuantity
field equal to theQuantity
property of thereceivedOrderDetails
object.this.orderId = receivedOrderDetails.ItemId; this.orderItem = receivedOrderDetails.Item; this.orderQuantity = receivedOrderDetails.Quantity;
Set the
orderItemStatus
field equal to the value,Received Order
.this.orderItemStatus = "Received order";
Compiling the Code
For more information about compiling your code, see Compiling the Code.
The SetStateActivity activity sets the current state of the state machine to the OrderProcessingState
. In Task 5: Define the OrderProcessing State, you add child activities to define the OrderProcessingState
.
See Also
Reference
EventDrivenActivity
HandleExternalEventActivity
CallExternalMethodActivity
SetStateActivity
ActivityBind
WorkflowParameterBinding
SetBinding
MethodInvoking
Concepts
Windows Workflow Foundation and Application Communication
State Machine Workflows
Other Resources
Task 5: Define the OrderProcessing State
Tutorial: Create a State Machine Workflow
Communications
Host Communication Sample
Ordering State Machine
Simple State Machine
Copyright © 2007 by Microsoft Corporation. All rights reserved.
Last Published: 2010-03-04