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.

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 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 SimpleExpenseReport 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
        Sub GetLeadApproval(ByVal message As String)
        Sub GetManagerApproval(ByVal message As String)
        Event ExpenseReportApproved As EventHandler(Of ExternalDataEventArgs)
        Event ExpenseReportRejected As EventHandler(Of ExternalDataEventArgs)
    End 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.

    NoteNote

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

    Public Class MainForm : Inherits Form : Implements IExpenseReportService
    
    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 Sub GetApprovalDelegate(ByVal message As String)
    
    private delegate void GetApprovalDelegate(string message);
    
  3. In the MainForm class, create a private generic EventHandler event of the ExternalDataEventArgs type named reportApproved.

    Private Event reportApproved As EventHandler(Of ExternalDataEventArgs) _
        Implements IExpenseReportService.ExpenseReportApproved
    
    private event EventHandler<ExternalDataEventArgs> reportApproved;
    
  4. In the MainForm class, create a private generic EventHandler event of the ExternalDataEventArgs type named reportRejected.

    Private Event reportRejected As EventHandler(Of ExternalDataEventArgs) _
        Implements IExpenseReportService.ExpenseReportRejected
    
    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 Custom Event ExpenseReportApproved As EventHandler(Of ExternalDataEventArgs)
        AddHandler(ByVal d As EventHandler(Of ExternalDataEventArgs))
            AddHandler reportApproved, d
        End AddHandler
        RemoveHandler(ByVal d As EventHandler(Of ExternalDataEventArgs))
            RemoveHandler reportApproved, d
        End RemoveHandler
        RaiseEvent(ByVal o As System.Object, ByVal e As ExternalDataEventArgs)
            RaiseEvent reportApproved(o, e)
        End RaiseEvent
    End 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 Custom Event ExpenseReportRejected As EventHandler(Of ExternalDataEventArgs)
        AddHandler(ByVal d As EventHandler(Of ExternalDataEventArgs))
            AddHandler reportRejected, d
        End AddHandler
        RemoveHandler(ByVal d As EventHandler(Of ExternalDataEventArgs))
            RemoveHandler reportRejected, d
        End RemoveHandler
        RaiseEvent(ByVal o As System.Object, ByVal e As ExternalDataEventArgs)
            RaiseEvent reportRejected(o, e)
        End RaiseEvent
    End 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 exchangeService As ExternalDataExchangeService = Nothing
    
    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 passing 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.

    Me.workflowRuntime = New WorkflowRuntime()
    Me.exchangeService = New ExternalDataExchangeService()
    workflowRuntime.AddService(exchangeService)
    exchangeService.AddService(Me)
    workflowRuntime.StartRuntime()
    
    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 Sub GetLeadApproval(ByVal message As String) _
              Implements IExpenseReportService.GetLeadApproval
    End Sub
    
    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 Sub GetManagerApproval(ByVal message As String) _
              Implements IExpenseReportService.GetManagerApproval
    End Sub
    
    public void GetManagerApproval(string message)
    {
    }
    

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.

    NoteNote

    At this point, you may see two warnings when you build the project. The ExpenseReportApproved and ExpenseReportRejected events will be 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
Workflow and Application Communication

Other Resources

Exercise 3: Create the Simple Expense Report Sequential Workflow

Footer image

Send comments about this topic to Microsoft.