Task 2: Define the IExpenseReportService Interface
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
In the SimpleExpenseReport source code file, create a public interface named IExpenseReportService and add the ExternalDataExchangeAttribute attribute to that interface.
Add a method named GetLeadApproval that accepts a String parameter named message to the IExpenseReportService interface.
Add a method named GetManagerApproval that accepts a String parameter named message to the IExpenseReportService interface.
Add a generic EventHandler event of the ExternalDataEventArgs type named ExpenseReportApproved.
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
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 : Inherits Form : Implements IExpenseReportService
public class MainForm : Form, IExpenseReportService
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);
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;
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;
In the MainForm class, create a public generic EventHandler event property of the ExternalDataEventArgs type named ExpenseReportApproved.
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; } }
In the MainForm class, create a public generic EventHandler event property of the ExternalDataEventArgs type named ExpenseReportRejected.
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; } }
In the MainForm class, create a private ExternalDataExchangeService field named exchangeService.
Private exchangeService As ExternalDataExchangeService = Nothing
private ExternalDataExchangeService exchangeService = null;
In the MainForm constructor, create a new instance of the exchangeService object following the creation of the workflowRuntime object.
Call the AddService method of the workflowRuntime object passing in the exchangeService object as a parameter.
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();
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) { }
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
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.
Note 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
Send comments about this topic to Microsoft.