Task 2: Define the IOrderingService Interface
In this task, you define the IOrderingService
interface and a class that is used to pass data from the host application to the state machine workflow when a new order is submitted.
The IOrderingService
interface is used to make communication easier between the Simple Order Form host application and the OrderProcessingWorkflow
.
The interface contains a single method and a defined event:
The
ItemStatusUpdate
method is used by theOrderProcessingWorkflow
to send status updates back to the host application.The
NewOrder
event is the initial event that starts theWaitingForOrder
state activity. It is raised by the Simple Order Form host application whenever the user submits a new order.
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 section.
Defining the NewOrderEventArgs Class
First, you must define the class that is used to pass data from the host application to the state machine workflow when an order is submitted. This class derives from ExternalDataEventArgs, the base class for data objects that store data contained in events raised by the host.
To define the NewOrderEventArgs class
In the StateMachineWorkflow project, create a new class file named NewOrderEventArgs.cs. Make the class
public
, and addExternalDataEventArgs
as the base class for this new class. Add a reference toSystem.Workflow.Activities
to the declarations in the file using the Options menu available on the class name.Make the class serializable by applying the SerializableAttribute attribute.
<Serializable()> _ Public Class NewOrderEventArgs : Inherits ExternalDataEventArgs End Class
[Serializable] public class NewOrderEventArgs : ExternalDataEventArgs { }
In the
NewOrderEventArgs
class, declare the fields in the following table.Access Modifier
Type
Name
Private
System.Guid
orderItemId
Private
System.String
orderItem
Private
System.Int32
orderQuantity
private Guid orderItemId; private string orderItem; private int orderQuantity;
Create a public constructor for the
NewOrderEventArgs
class that takes the following as parameters:These parameters contain the workflow ID of the destination workflow, a string that defines the name of an item, and an integer value that defines an item quantity. In the body of the constructor, assign each field that you created in the previous step with the associated parameter passed into the constructor. This stores the data provided when the constructor is called.
public NewOrderEventArgs(Guid itemId, string item, int quantity) : base(itemId) { this.orderItemId = itemId; this.orderItem = item; this.orderQuantity = quantity; }
In the
NewOrderEventArgs
class, create a public System.Guid property namedItemId
.Create a
get
method for this property and return the value of theorderItemId
field, and aset
method that sets theorderItemId
field.public Guid ItemId { get { return orderItemId; } set { orderItemId = value; } }
In the
NewOrderEventArgs
class, create a public System.String property namedItem
.Create a
get
method for this property and return the value of theorderItem
field, and aset
method that sets theorderItem
field.public string Item { get { return orderItem; } set { orderItem = value; } }
In the
NewOrderEventArgs
class, create a public System.Int32 property namedQuantity
.Create a
get
method for this property and return the value of theorderQuantity
field, and aset
method that sets theorderQuantity
field.public int Quantity { get { return orderQuantity; } set { orderQuantity = value; } }
Creating the Interface
Next, you define the IOrderingService
interface. This is used for communication between the workflow and the host application. The workflow contains an activity that listens for events using this interface. The host sends messages to the workflow using a service that implements this interface.
To define the IOrderingService interface
In the StateMachineWorkflow project, create a new class file named
IOrderingService.cs
. Make the interfacepublic
.Add a
using
statement for theSystem.Workflow.Activities
namespace.Imports System.Workflow.Activities
using System.Workflow.Activities;
Because this interface is used for communication between the host application and the
OrderProcessingWorkflow
, apply the ExternalDataExchangeAttribute attribute. This attribute identifies the interface as a local service interface.<ExternalDataExchange()> _ Public Interface IOrderingService End Interface
[ExternalDataExchange] public interface IOrderingService { }
In the
IOrderingService
interface, create a method namedItemStatusUpdate
that takes the following as parameters:A Guid named
orderId
.A String named
newStatus
.
void ItemStatusUpdate(Guid orderId, string newStatus);
In the
IOrderingService
interface, create a new event namedNewOrder
whose type is a genericEventHandler
of theNewOrderEventArgs
type.event EventHandler<NewOrderEventArgs> NewOrder;
Compiling the Code
For more information about compiling your code, see Compiling the Code.
In the next task, Task 3: Implement the IOrderingService Interface, you implement this interface in the Simple Order Form host application.
See Also
Reference
Concepts
Windows Workflow Foundation and Application Communication
State Machine Workflows
Other Resources
Task 3: Implement the IOrderingService Interface
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