Compartilhar via


Task 4: Define the WaitingForOrder State

Download sample

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

  1. 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. The NewOrderEventArgs 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 the eventDriven1 activity to open it.)

    • InterfaceType: IOrderingService

    • EventName: NewOrder

    CallExternalMethodActivity

    updatestatusOrderReceived

    eventDriven1 (Double-click the eventDriven1 activity to open it.)

    • InterfaceType: IOrderingService

    • Method Name: ItemStatusUpdate

    SetStateActivity

    setStateActivityOrderProcessing

    eventDriven1 (Double-click the eventDriven1 activity to open it.)

    • TargetStateName: OrderProcessingStateActivity

    NewOrderEventArgs

    receivedOrderDetails

    OrderProcessingWorkflow (field)

    Guid

    orderId

    OrderProcessingWorkflow (field)

    String

    orderItem

    OrderProcessingWorkflow (field)

    Int32

    orderQuantity

    OrderProcessingWorkflow (field)

    String

    orderIemStatus

    OrderProcessingWorkflow (field)

    When the activities are defined, the workflow designer should look like the following illustration.

    Workflow Designer Screenshot

    Expanding the eventDriven1 activity shows the following:

    WaitingForOrderState child activities

  2. Next, create properties for accessing the fields. Create a Guid property named Id and define a GET method that returns the value of the orderId field.

    public Guid Id
    {
        get
        {
            return this.orderId;
        }
    }
    
  3. Create a String property named ItemStatus and define a get method that returns the value of the orderItemStatus field.

    public string ItemStatus
    {
        get
        {
            return this.orderItemStatus;
        }
    }
    
  4. Create a String property named Item and define a get method that returns the value of the orderItem field.

    public string Item
    {
        get
        {
            return this.orderItem;
        }
    }
    

Creating the OrderReceived Method

To create the OrderReceived method

  1. Create the handler that executes when the order is received by creating a new private method named OrderReceived that accepts a Object method named sender and an EventArgs object named e. These are the two standard parameters for event handlers.

    private void OrderReceived(object sender, EventArgs e)
    
  2. When this event fires, the incoming information from the event is stored in the variables that were previously created. In the OrderReceived method, set the orderId field equal to the ItemId property of the receivedOrderDetails object.

  3. Set the orderItem field equal to the Item property of the receivedOrderDetails object.

  4. Set the orderQuantity field equal to the Quantity property of the receivedOrderDetails object.

    this.orderId = receivedOrderDetails.ItemId;
    this.orderItem = receivedOrderDetails.Item;
    this.orderQuantity = receivedOrderDetails.Quantity;
    
  5. 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