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 ExpenseReportWorkflow source code file, create a
public
interface namedIExpenseReportService
and add the ExternalDataExchangeAttribute attribute to that interface.Add a method named
GetLeadApproval
that accepts a String parameter namedmessage
to theIExpenseReportService
interface.Add a method named
GetManagerApproval
that accepts a String parameter namedmessage
to theIExpenseReportService
interface.Add a generic
EventHandler
event of the ExternalDataEventArgs type namedExpenseReportApproved
.Add a generic
EventHandler
event of the ExternalDataEventArgs type namedExpenseReportRejected
.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
Modify the declaration of the
MainForm
class so that it implements theIExpenseReportService
interface.Note
This step is in addition to the
Form
class that yourMainForm
class already derives from.public class MainForm : Form, IExpenseReportService
In the
MainForm
class, create aprivate
delegate method namedGetApprovalDelegate
that accepts a String namedmessage
.private delegate void GetApprovalDelegate(string message);
The
GetApprovalDelegate
delegate is used in the next exercise when you implement methods on theIExpenseReportService
interface.In the
MainForm
class, create aprivate
genericEventHandler
event of the ExternalDataEventArgs type namedreportApproved
.private event EventHandler<ExternalDataEventArgs> reportApproved;
In the
MainForm
class, create aprivate
genericEventHandler
event of the ExternalDataEventArgs type namedreportRejected
.private event EventHandler<ExternalDataEventArgs> reportRejected;
In the
MainForm
class, create apublic
genericEventHandler
event property of the ExternalDataEventArgs type namedExpenseReportApproved
.In the
ExpenseReportApproved
event property, create anadd
andremove
accessor to add and remove event handlers from thereportApproved
event.public event EventHandler<ExternalDataEventArgs> ExpenseReportApproved { add { reportApproved += value; } remove { reportApproved -= value; } }
In the
MainForm
class, create a public genericEventHandler
event property of the ExternalDataEventArgs type namedExpenseReportRejected
.In the
ExpenseReportRejected
event property, create anadd
andremove
accessor to add and remove event handlers from thereportRejected
event.public event EventHandler<ExternalDataEventArgs> ExpenseReportRejected { add { reportRejected += value; } remove { reportRejected -= value; } }
In the
MainForm
class, create a private ExternalDataExchangeService field namedexchangeService
.private ExternalDataExchangeService exchangeService = null;
In the
MainForm
constructor, create a new instance of theexchangeService
object following the creation of theworkflowRuntime
object.Call the AddService method of the
workflowRuntime
object and pass in theexchangeService
object as a parameter.Call the AddService method of the
exchangeService
object, passing in the instance of theMainForm
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();
In the
MainForm
class, create apublic
method namedGetLeadApproval
that accepts a String namedmessage
as a parameter.public void GetLeadApproval(string message) { }
In the
MainForm
class, create apublic
method namedGetManagerApproval
that accepts a String namedmessage
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