Partager via


Task 2: Define the IOrderingService Interface

Download sample

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 the OrderProcessingWorkflow to send status updates back to the host application.

  • The NewOrder event is the initial event that starts the WaitingForOrder 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

  1. In the StateMachineWorkflow project, create a new class file named NewOrderEventArgs.cs. Make the class public, and add ExternalDataEventArgs as the base class for this new class. Add a reference to System.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
    {
    }
    
  2. 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;
    
  3. 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;
    }
    
  4. In the NewOrderEventArgs class, create a public System.Guid property named ItemId.

    Create a get method for this property and return the value of the orderItemId field, and a set method that sets the orderItemId field.

    public Guid ItemId
    {
        get { return orderItemId; }
        set { orderItemId = value; }
    }
    
  5. In the NewOrderEventArgs class, create a public System.String property named Item.

    Create a get method for this property and return the value of the orderItem field, and a set method that sets the orderItem field.

    public string Item
    {
        get { return orderItem; }
        set { orderItem = value; }
    }
    
  6. In the NewOrderEventArgs class, create a public System.Int32 property named Quantity.

    Create a get method for this property and return the value of the orderQuantity field, and a set method that sets the orderQuantity 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

  1. In the StateMachineWorkflow project, create a new class file named IOrderingService.cs. Make the interface public.

  2. Add a using statement for the System.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
    {
    }
    
  3. In the IOrderingService interface, create a method named ItemStatusUpdate that takes the following as parameters:

    • A Guid named orderId.

    • A String named newStatus.

    void ItemStatusUpdate(Guid orderId, string newStatus);
    
  4. In the IOrderingService interface, create a new event named NewOrder whose type is a generic EventHandler of the NewOrderEventArgs 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

ExternalDataExchangeAttribute

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