Partager via


Task 2: Define the IExpenseReportService Interface

Download sample

In this task, you create the IExpenseReportService interface. This interface enables the workflow and host application to communicate by using a predefined protocol. The interface contains two methods that the workflow uses to call a method that is defined in the host application. Additionally, two events are defined for the host application to notify the workflow when certain events occur.

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 then proceeding to the steps in the following section.

Defining and Implementing the Interface

First, you define the interface as shown in the following procedure.

To define the IExpenseReportService interface

  1. In the ExpenseReportWorkflow source code file, create a public interface named IExpenseReportService and add the ExternalDataExchangeAttribute attribute to that interface.

  2. Add a method named GetLeadApproval that accepts a String parameter named message to the IExpenseReportService interface.

  3. Add a method named GetManagerApproval that accepts a String parameter named message to the IExpenseReportService interface.

  4. Add a generic EventHandler event of the ExternalDataEventArgs type named ExpenseReportApproved.

  5. Add a generic EventHandler event of the ExternalDataEventArgs type named ExpenseReportRejected.

    The following code shows the completed definition of the IExpenseReportService interface.

    [ExternalDataExchange]
    public interface IExpenseReportService
    {
        void GetLeadApproval(string message);
        void GetManagerApproval(string message);
        event EventHandler<ExternalDataEventArgs> ExpenseReportApproved;
        event EventHandler<ExternalDataEventArgs> ExpenseReportRejected;
    }
    

Implementing the Interface

Next, you modify the MainForm class so that it implements the IExpenseReportService interface.

To implement the IExpenseReportService interface

  1. Modify the declaration of the MainForm class so that it implements the IExpenseReportService interface.

    Note

    This step is in addition to the Form class that your MainForm class already derives from.

    public class MainForm : Form, IExpenseReportService
    
  2. In the MainForm class, create a private delegate method named GetApprovalDelegate that accepts a String named message.

    private delegate void GetApprovalDelegate(string message);
    

    The GetApprovalDelegate delegate is used in the next exercise when you implement methods on the IExpenseReportService interface.

  3. In the MainForm class, create a private generic EventHandler event of the ExternalDataEventArgs type named reportApproved.

    private event EventHandler<ExternalDataEventArgs> reportApproved;
    
  4. In the MainForm class, create a private generic EventHandler event of the ExternalDataEventArgs type named reportRejected.

    private event EventHandler<ExternalDataEventArgs> reportRejected;
    
  5. In the MainForm class, create a public generic EventHandler event property of the ExternalDataEventArgs type named ExpenseReportApproved.

  6. In the ExpenseReportApproved event property, create an add and remove accessor to add and remove event handlers from the reportApproved event.

    public event EventHandler<ExternalDataEventArgs> ExpenseReportApproved
    {
        add
        {
            reportApproved += value;
        }
        remove
        {
            reportApproved -= value;
        }
    }
    
  7. In the MainForm class, create a public generic EventHandler event property of the ExternalDataEventArgs type named ExpenseReportRejected.

  8. In the ExpenseReportRejected event property, create an add and remove accessor to add and remove event handlers from the reportRejected event.

    public event EventHandler<ExternalDataEventArgs> ExpenseReportRejected
    {
        add
        {
            reportRejected += value;
        }
        remove
        {
            reportRejected -= value;
        }
    }
    
  9. In the MainForm class, create a private ExternalDataExchangeService field named exchangeService.

    private ExternalDataExchangeService exchangeService = null;
    
  10. In the MainForm constructor, create a new instance of the exchangeService object following the creation of the workflowRuntime object.

  11. Call the AddService method of the workflowRuntime object and pass in the exchangeService object as a parameter.

  12. Call the AddService method of the exchangeService object, passing in the instance of the MainForm class as a parameter. The following code example shows how your form, acting as your local communication service, is added to the collection of communication services in ExternalDataExchangeService, which are then used by the WorkflowRuntime during local host communication.

    this.workflowRuntime = new WorkflowRuntime();
    
    this.exchangeService = new ExternalDataExchangeService();
    workflowRuntime.AddService(exchangeService);
    exchangeService.AddService(this);
    workflowRuntime.StartRuntime();
    
  13. In the MainForm class, create a public method named GetLeadApproval that accepts a String named message as a parameter.

    public void GetLeadApproval(string message)
    {
    }
    
  14. In the MainForm class, create a public method named GetManagerApproval that accepts a String named message as a parameter.

    public void GetManagerApproval(string message)
    {
    }
    

Compiling the Code

For more information about compiling your code, see Compiling the Code.

Note

At this point, you may see two warnings when you build the project. The ExpenseReportApproved and ExpenseReportRejected events are used in the next task.

In Exercise 3: Create the Simple Expense Report Sequential Workflow, you will finish the sequential workflow by adding activities that communicate with the host application.

See Also

Reference

ExternalDataExchangeAttribute
ExternalDataEventArgs

Concepts

Using Local Services in Workflows
Windows Workflow Foundation and Application Communication

Other Resources

Exercise 3: Create the Simple Expense Report Sequential Workflow

Copyright © 2007 by Microsoft Corporation. All rights reserved.
Last Published: 2010-03-04