Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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
OrderProcessingWorkflowworkflow 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. TheNewOrderEventArgsobject 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
eventDriven1WaitingForOrderStateHandleExternalEventActivity
newOrderExternalEventeventDriven1(Double-click theeventDriven1activity to open it.)- InterfaceType: IOrderingService
- EventName: NewOrder
CallExternalMethodActivity
updatestatusOrderReceivedeventDriven1(Double-click theeventDriven1activity to open it.)- InterfaceType: IOrderingService
- Method Name: ItemStatusUpdate
SetStateActivity
setStateActivityOrderProcessingeventDriven1(Double-click theeventDriven1activity to open it.)- TargetStateName: OrderProcessingStateActivity
NewOrderEventArgsreceivedOrderDetailsOrderProcessingWorkflow (field)
orderIdOrderProcessingWorkflow (field)
orderItemOrderProcessingWorkflow (field)
orderQuantityOrderProcessingWorkflow (field)
String
orderIemStatusOrderProcessingWorkflow (field)
When the activities are defined, the workflow designer should look like the following illustration.
.jpg)
Expanding the
eventDriven1activity shows the following:.jpg)
Next, create properties for accessing the fields. Create a Guid property named
Idand define aGETmethod that returns the value of theorderIdfield.public Guid Id { get { return this.orderId; } }Create a String property named
ItemStatusand define agetmethod that returns the value of theorderItemStatusfield.public string ItemStatus { get { return this.orderItemStatus; } }Create a String property named
Itemand define agetmethod that returns the value of theorderItemfield.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
OrderReceivedthat accepts a Object method namedsenderand anEventArgsobject 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
OrderReceivedmethod, set theorderIdfield equal to theItemIdproperty of thereceivedOrderDetailsobject.Set the
orderItemfield equal to theItemproperty of thereceivedOrderDetailsobject.Set the
orderQuantityfield equal to theQuantityproperty of thereceivedOrderDetailsobject.this.orderId = receivedOrderDetails.ItemId; this.orderItem = receivedOrderDetails.Item; this.orderQuantity = receivedOrderDetails.Quantity;Set the
orderItemStatusfield 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