Task 4: Define the WaitingForOrder StateĀ
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.
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. |
Declaring Workflow Fields and Properties
.
To declare new OrderProcessingWorkflow fields and properties
In the OrderProcessingWorkflow class, create the following fields:
Access Modifier Type Name Private
eventDriven1
Private
newOrderExternalEvent
Private
updatestatusOrderReceived
Private
SetStateActivity
setStateActivityOrderProcessing
Public
NewOrderEventArgs
receivedOrderDetails
Public
orderId
Private
orderItem
Private
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;
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; } }
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; } }
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
In the InitializeComponent method of the OrderProcessingWorkflow class, declare the following local variables, and create an instance of each one:
Important These variables must be declared after the code that sets the CanModifyActivities property to true.
Type Name activitybind1
ActivityBind
activitybind2
ActivityBind
activitybind3
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();
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();
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);
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.
Important Make sure that you add them in the order mentioned in this step.
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";
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.
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);
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";
Set the ParameterName property of the workflowparameterbinding1 object to the value, "e".
Call the SetBinding method of the workflowparameterbinding1 object, passing the static property ValueProperty and the activitybind1 object as parameters.
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);
Set the Name property of the newOrderExternalEvent object to the value, "newOrderExternalEvent".
Me.newOrderExternalEvent.Name = "newOrderExternalEvent"
this.newOrderExternalEvent.Name = "newOrderExternalEvent";
Set the InterfaceType property of the updatestatusOrderReceived activity equal to the IOrderingService type.
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.
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";
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";
Set the ParameterName property of the workflowparameterbinding2 object to the value, "orderId".
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)));
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";
Set the ParameterName property of the workflowparameterbinding3 object to the value, "newStatus".
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)));
Add the workflowparameterbinding2 object to the ParameterBindings collection of the updateStatusOrderReceived object.
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);
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);
Set the Name property of the setStateActivityOrderProcessing activity to the value, "setStateActivityOrderProcessing".
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
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)
In the OrderReceived method, set the orderId field equal to the ItemId property of the receivedOrderDetails object.
Set the orderItem field equal to the Item property of the receivedOrderDetails object.
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;
Set the orderItemStatus field equal to the value, "Received Order".
Me.orderItemStatus = "Received order"
this.orderItemStatus = "Received order";
Compiling the Code
Click Start, point to Programs, point to Microsoft .NET Framework SDK v2.0, and then click SDK Command Prompt.
Go to the source directory of the tutorial.
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
Send comments about this topic to Microsoft.