Share via


Task 4: Define the WaitingForOrder StateĀ 

Download sample

In this task, you define the first state in the OrderProcessingWorkflow 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. This indicates that the order was received.

The last step is to transition to the next stage in the state machine by using a SetStateActivity.

NoteNote

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.

Declaring Workflow Fields and Properties

.

To declare new OrderProcessingWorkflow fields and properties

  1. In the OrderProcessingWorkflow class, create the following fields:

    Access Modifier Type Name

    Private

    EventDrivenActivity

    eventDriven1

    Private

    HandleExternalEventActivity

    newOrderExternalEvent

    Private

    CallExternalMethodActivity

    updatestatusOrderReceived

    Private

    SetStateActivity

    setStateActivityOrderProcessing

    Public

    NewOrderEventArgs

    receivedOrderDetails

    Public

    Guid

    orderId

    Private

    String

    orderItem

    Private

    Int32

    orderQuantity

    Public

    String

    orderIemStatus

    Private eventDriven1 As EventDrivenActivity
    Private newOrderExternalEvent As HandleExternalEventActivity
    Private updatestatusOrderReceived As CallExternalMethodActivity
    Private setStateActivityOrderProcessing As SetStateActivity
    Public receivedOrderDetails As NewOrderEventArgs
    
    Public orderId As Guid
    Private orderItem As String
    Private orderQuantity As Integer
    Public orderItemStatus As String
    
    // WaitingForOrderStateActivity Activities
    private EventDrivenActivity eventDriven1;
    private HandleExternalEventActivity newOrderExternalEvent;
    private CallExternalMethodActivity updatestatusOrderReceived;
    private SetStateActivity setStateActivityOrderProcessing;
    public NewOrderEventArgs receivedOrderDetails = null;
    
    public Guid orderId;
    private string orderItem;
    private int orderQuantity;
    public string orderItemStatus;
    
  2. Create a System.Guid property named Id and define a get method that returns the value of the orderId field.

    Public ReadOnly Property Id() As Guid
        Get
            Return Me.orderId
        End Get
    End Property
    
    public Guid Id
    {
        get
        {
            return this.orderId;
        }
    }
    
  3. Create a System.String property named ItemStatus, and define a get method that returns the value of the orderItemStatus field.

    Public ReadOnly Property ItemStatus() As String
        Get
            Return Me.orderItemStatus
        End Get
    End Property
    
    public string ItemStatus
    {
        get
        {
            return this.orderItemStatus;
        }
    }
    
  4. Create a System.String property named Item, and define a get method that returns the value of the orderItem field.

    Public ReadOnly Property Item() As String
        Get
            Return Me.orderItem
        End Get
    End Property
    
    public string Item
    {
        get
        {
            return this.orderItem;
        }
    }
    

Defining the Child Activities and Creating the Method

Next, you add several child activities that define WaitingForOrderState. You transition to the next stage in the state machine by using a SetStateActivity.

To define the WaitingForOrderState child activities

  1. In the InitializeComponent method of the OrderProcessingWorkflow class, declare the following local variables, and create an instance of each one:

    NoteImportant

    These variables must be declared after the code that sets the CanModifyActivities property to true.

    Type Name

    ActivityBind

    activitybind1

    ActivityBind

    activitybind2

    ActivityBind

    activitybind3

    WorkflowParameterBinding

    workflowparameterbinding1

    WorkflowParameterBinding

    workflowparameterbinding2

    WorkflowParameterBinding

    workflowparameterbinding3

    Dim activitybind1 As System.Workflow.ComponentModel.ActivityBind = _
        New System.Workflow.ComponentModel.ActivityBind()
    Dim activitybind2 As System.Workflow.ComponentModel.ActivityBind = _
        New System.Workflow.ComponentModel.ActivityBind()
    Dim activitybind3 As System.Workflow.ComponentModel.ActivityBind = _
        New System.Workflow.ComponentModel.ActivityBind()
    Dim workflowparameterbinding1 As _
        System.Workflow.ComponentModel.WorkflowParameterBinding = _
        New System.Workflow.ComponentModel.WorkflowParameterBinding()
    Dim workflowparameterbinding2 As _
        System.Workflow.ComponentModel.WorkflowParameterBinding = _
        New System.Workflow.ComponentModel.WorkflowParameterBinding()
    Dim workflowparameterbinding3 As _
        System.Workflow.ComponentModel.WorkflowParameterBinding = _
        New System.Workflow.ComponentModel.WorkflowParameterBinding()
    
    ActivityBind activitybind1 = new ActivityBind();
    ActivityBind activitybind2 = new ActivityBind();
    ActivityBind activitybind3 = new ActivityBind();
    WorkflowParameterBinding workflowparameterbinding1 = 
        new WorkflowParameterBinding();
    WorkflowParameterBinding workflowparameterbinding2 = 
        new WorkflowParameterBinding();
    WorkflowParameterBinding workflowparameterbinding3 = 
        new WorkflowParameterBinding();
    
  2. Create an instance of the eventDriven1, newOrderExternalEvent, updateStatusOrderReceived, and setStateActivityOrderProcessing objects after the code that creates the three states of the state machine.

    ' WaitingForOrderStateActivity Activities
    Me.eventDriven1 = New System.Workflow.Activities.EventDrivenActivity()
    Me.newOrderExternalEvent = New _
        System.Workflow.Activities.HandleExternalEventActivity()
    Me.updatestatusOrderReceived = New _
        System.Workflow.Activities.CallExternalMethodActivity()
    Me.setStateActivityOrderProcessing = New _
        System.Workflow.Activities.SetStateActivity()
    
    // WaitingForOrderStateActivity child activities
    this.eventDriven1 = new EventDrivenActivity();
    this.newOrderExternalEvent = new HandleExternalEventActivity();
    this.updatestatusOrderReceived = new CallExternalMethodActivity();
    this.setStateActivityOrderProcessing = new SetStateActivity();
    
  3. Add the eventDriven1 object to the Activities collection of the WaitingForOrderStateActivity.

    Use the Add method that is defined in the Activities collection, and pass the eventDriven1 object as a parameter. This should be added after the code that initializes the WaitingForOrderStateActivity.

    Me.WaitingForOrderStateActivity.Activities.Add(Me.eventDriven1)
    
    this.WaitingForOrderStateActivity.Activities.Add(this.eventDriven1);
    
  4. After the code that sets the Name property of the OrderCompletedStateActivity, add the newOrderExternalEvent, updateStatusOrderReceived, and the setStateActivityOrderCompleted activities to the Activities collection of the eventDriven1 activity.

    NoteImportant

    Make sure that you add them in the order mentioned in this step.

  5. Set the Name property of the eventDriven1 activity to the value, "eventDriven1".

    ' 
    ' eventDriven1
    ' 
    Me.eventDriven1.Activities.Add(Me.newOrderExternalEvent)
    Me.eventDriven1.Activities.Add(Me.updatestatusOrderReceived)
    Me.eventDriven1.Activities.Add(Me.setStateActivityOrderProcessing)
    Me.eventDriven1.Name = "eventDriven1"
    
    // 
    // eventDriven1
    // 
    this.eventDriven1.Activities.Add(this.newOrderExternalEvent);
    this.eventDriven1.Activities.Add(this.updatestatusOrderReceived);
    this.eventDriven1.Activities.Add(this.setStateActivityOrderProcessing);
    this.eventDriven1.Name = "eventDriven1";
    
  6. Set the EventName property of the newOrderExternalEvent object equal to the value "NewOrder".

    This is the event name that is defined in the IOrderingService, and it is the event that newOrderExternalEvent waits for from the host application.

  7. Set the InterfaceType property of the newOrderExternalEvent activity equal to the IOrderingService type.

    Me.newOrderExternalEvent.EventName = "NewOrder"
    Me.newOrderExternalEvent.InterfaceType = _
        GetType(StateMachineWorkflow.IOrderingService)
    
    this.newOrderExternalEvent.EventName = "NewOrder";
    this.newOrderExternalEvent.InterfaceType = typeof(IOrderingService);
    
  8. Set the Name property of the activitybind1 object to "OrderProcessingWorkflow" and the Path property of the activitybind1 object to the value, "receivedOrderDetails".

    activitybind1.Name = "OrderProcessingWorkflow"
    activitybind1.Path = "receivedOrderDetails"
    
    activitybind1.Name = "OrderProcessingWorkflow";
    activitybind1.Path = "receivedOrderDetails";
    
  9. Set the ParameterName property of the workflowparameterbinding1 object to the value, "e".

  10. Call the SetBinding method of the workflowparameterbinding1 object, passing the static property ValueProperty and the activitybind1 object as parameters.

  11. Add the workflowparameterbinding1 object to the ParameterBindings collection of the newOrderExternalEvent object.

    workflowparameterbinding1.ParameterName = "e"
    workflowparameterbinding1.SetBinding( _
        System.Workflow.ComponentModel.WorkflowParameterBinding.ValueProperty, _
        (CType(activitybind1, System.Workflow.ComponentModel.ActivityBind)))
    Me.newOrderExternalEvent.ParameterBindings.Add(workflowparameterbinding1)
    
    workflowparameterbinding1.ParameterName = "e";
    workflowparameterbinding1.SetBinding(
        System.Workflow.ComponentModel.WorkflowParameterBinding.ValueProperty,
        ((System.Workflow.ComponentModel.ActivityBind)(activitybind1)));
    this.newOrderExternalEvent.ParameterBindings.Add(workflowparameterbinding1);
    
  12. Set the Name property of the newOrderExternalEvent object to the value, "newOrderExternalEvent".

    Me.newOrderExternalEvent.Name = "newOrderExternalEvent"
    
    this.newOrderExternalEvent.Name = "newOrderExternalEvent";
    
  13. Set the InterfaceType property of the updatestatusOrderReceived activity equal to the IOrderingService type.

  14. Set the MethodName property of the updatestatusOrderReceived object equal to the value "ItemStatusUpdate".

    This is the method name that is defined in the IOrderingService, and it is implemented by the host application.

  15. Set the Name property of the updatestatusOrderReceived object to the value, " updatestatusOrderReceived".

    Me.updatestatusOrderReceived.InterfaceType = _
        GetType(StateMachineWorkflow.IOrderingService)
    Me.updatestatusOrderReceived.MethodName = "ItemStatusUpdate"
    Me.updatestatusOrderReceived.Name = "updatestatusOrderReceived"
    
    this.updatestatusOrderReceived.InterfaceType = typeof(IOrderingService);
    this.updatestatusOrderReceived.MethodName = "ItemStatusUpdate";
    this.updatestatusOrderReceived.Name = "updatestatusOrderReceived";
    
  16. Set the Name property of the activitybind2 object to "OrderProcessingWorkflow" and the Path property of the activitybind2 object to the value, "orderId".

    activitybind2.Name = "OrderProcessingWorkflow"
    activitybind2.Path = "orderId"
    
    activitybind2.Name = "OrderProcessingWorkflow";
    activitybind2.Path = "orderId";
    
  17. Set the ParameterName property of the workflowparameterbinding2 object to the value, "orderId".

  18. Call the SetBinding method of the workflowparameterbinding2 object, passing the static property ValueProperty and the activitybind2 object as parameters.

    workflowparameterbinding2.ParameterName = "orderId"
    workflowparameterbinding2.SetBinding( _
        System.Workflow.ComponentModel.WorkflowParameterBinding.ValueProperty, _
        (CType(activitybind2, System.Workflow.ComponentModel.ActivityBind)))
    
    workflowparameterbinding2.ParameterName = "orderId";
    workflowparameterbinding2.SetBinding(WorkflowParameterBinding.ValueProperty,
        ((System.Workflow.ComponentModel.ActivityBind)(activitybind2)));
    
  19. Set the Name property of the activitybind3 object to "OrderProcessingWorkflow" and the Path property of the activitybind3 object to the value, "orderItemStatus".

    activitybind3.Name = "OrderProcessingWorkflow"
    activitybind3.Path = "orderItemStatus"
    
    activitybind3.Name = "OrderProcessingWorkflow";
    activitybind3.Path = "orderItemStatus";
    
  20. Set the ParameterName property of the workflowparameterbinding3 object to the value, "newStatus".

  21. Call the SetBinding method of the workflowparameterbinding3 object, passing the static property ValueProperty and the activitybind3 object as parameters.

    workflowparameterbinding3.ParameterName = "newStatus"
    workflowparameterbinding3.SetBinding( _
    System.Workflow.ComponentModel.WorkflowParameterBinding.ValueProperty, _
    (CType(activitybind3, System.Workflow.ComponentModel.ActivityBind)))
    
    workflowparameterbinding3.ParameterName = "newStatus";
    workflowparameterbinding3.SetBinding(WorkflowParameterBinding.ValueProperty,
        ((System.Workflow.ComponentModel.ActivityBind)(activitybind3)));
    
  22. Add the workflowparameterbinding2 object to the ParameterBindings collection of the updateStatusOrderReceived object.

  23. Add the workflowparameterbinding1 object to the ParameterBindings collection of the updateStatusOrderReceived object.

    Me.updatestatusOrderReceived.ParameterBindings.Add(workflowparameterbinding2)
    Me.updatestatusOrderReceived.ParameterBindings.Add(workflowparameterbinding3)
    
    this.updatestatusOrderReceived.ParameterBindings.Add
        (workflowparameterbinding2);
    this.updatestatusOrderReceived.ParameterBindings.Add
        (workflowparameterbinding3);
    
  24. Add an event handler for the MethodInvoking event of the updateStatusOrderReceived object named OrderReceived.

    AddHandler updatestatusOrderReceived.MethodInvoking, _
        AddressOf Me.OrderReceived
    
    this.updatestatusOrderReceived.MethodInvoking += 
        new System.EventHandler(this.OrderReceived);
    
  25. Set the Name property of the setStateActivityOrderProcessing activity to the value, "setStateActivityOrderProcessing".

  26. Set the TargetStateName of the setStateActivityOrderProcessing activity to the value, "OrderProcessingStateActivity".

    Me.setStateActivityOrderProcessing.Name = "setStateActivityOrderProcessing"
    Me.setStateActivityOrderProcessing.TargetStateName = _
    "OrderProcessingStateActivity"
    
    // 
    // setStateActivityOrderProcessing
    // 
    this.setStateActivityOrderProcessing.Name = 
        "setStateActivityOrderProcessing";
    this.setStateActivityOrderProcessing.TargetStateName = 
        "OrderProcessingStateActivity";
    

To create the OrderReceived method

  1. Create a new private method named OrderReceived that accepts a System.Object method named sender and an EventArgs object named e.

    Private Sub OrderReceived(ByVal sender As Object, ByVal e As EventArgs)
    
    private void OrderReceived(object sender, EventArgs e)
    
  2. 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.

    Me.orderId = receivedOrderDetails.ItemId
    Me.orderItem = receivedOrderDetails.Item
    Me.orderQuantity = receivedOrderDetails.Quantity
    
    this.orderId = receivedOrderDetails.ItemId;
    this.orderItem = receivedOrderDetails.Item;
    this.orderQuantity = receivedOrderDetails.Quantity;
    
  5. Set the orderItemStatus field equal to the value, "Received Order".

    Me.orderItemStatus = "Received order"
    
    this.orderItemStatus = "Received order";
    

Compiling the Code

  1. Click Start, point to Programs, point to Microsoft .NET Framework SDK v2.0, and then click SDK Command Prompt.

  2. Go to the source directory of the tutorial.

  3. At the command prompt, type MSBUILD to build the project.

The SetStateActivity activity sets the current state of the state machine to the OrderProcessingState. In Task 5: Define the OrderProcessing State, you will add child activities to define the OrderProcessingState.

See Also

Reference

EventDrivenActivity
HandleExternalEventActivity
CallExternalMethodActivity
SetStateActivity
ActivityBind
WorkflowParameterBinding
SetBinding
MethodInvoking

Concepts

Workflow and Application Communication
State Machine Workflows

Other Resources

Task 5: Define the OrderProcessing State
Tutorial: Create a State Machine Workflow
Communications
HostCommunication Sample
Ordering State Machine
Simple State Machine

Footer image

Send comments about this topic to Microsoft.