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
publicinterface namedIExpenseReportServiceand add the ExternalDataExchangeAttribute attribute to that interface.Add a method named
GetLeadApprovalthat accepts a String parameter namedmessageto theIExpenseReportServiceinterface.Add a method named
GetManagerApprovalthat accepts a String parameter namedmessageto theIExpenseReportServiceinterface.Add a generic
EventHandlerevent of the ExternalDataEventArgs type namedExpenseReportApproved.Add a generic
EventHandlerevent of the ExternalDataEventArgs type namedExpenseReportRejected.The following code shows the completed definition of the
IExpenseReportServiceinterface.[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
MainFormclass so that it implements theIExpenseReportServiceinterface.Note
This step is in addition to the
Formclass that yourMainFormclass already derives from.public class MainForm : Form, IExpenseReportServiceIn the
MainFormclass, create aprivatedelegate method namedGetApprovalDelegatethat accepts a String namedmessage.private delegate void GetApprovalDelegate(string message);The
GetApprovalDelegatedelegate is used in the next exercise when you implement methods on theIExpenseReportServiceinterface.In the
MainFormclass, create aprivategenericEventHandlerevent of the ExternalDataEventArgs type namedreportApproved.private event EventHandler<ExternalDataEventArgs> reportApproved;In the
MainFormclass, create aprivategenericEventHandlerevent of the ExternalDataEventArgs type namedreportRejected.private event EventHandler<ExternalDataEventArgs> reportRejected;In the
MainFormclass, create apublicgenericEventHandlerevent property of the ExternalDataEventArgs type namedExpenseReportApproved.In the
ExpenseReportApprovedevent property, create anaddandremoveaccessor to add and remove event handlers from thereportApprovedevent.public event EventHandler<ExternalDataEventArgs> ExpenseReportApproved { add { reportApproved += value; } remove { reportApproved -= value; } }In the
MainFormclass, create a public genericEventHandlerevent property of the ExternalDataEventArgs type namedExpenseReportRejected.In the
ExpenseReportRejectedevent property, create anaddandremoveaccessor to add and remove event handlers from thereportRejectedevent.public event EventHandler<ExternalDataEventArgs> ExpenseReportRejected { add { reportRejected += value; } remove { reportRejected -= value; } }In the
MainFormclass, create a private ExternalDataExchangeService field namedexchangeService.private ExternalDataExchangeService exchangeService = null;In the
MainFormconstructor, create a new instance of theexchangeServiceobject following the creation of theworkflowRuntimeobject.Call the AddService method of the
workflowRuntimeobject and pass in theexchangeServiceobject as a parameter.Call the AddService method of the
exchangeServiceobject, passing in the instance of theMainFormclass 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
MainFormclass, create apublicmethod namedGetLeadApprovalthat accepts a String namedmessageas a parameter.public void GetLeadApproval(string message) { }In the
MainFormclass, create apublicmethod namedGetManagerApprovalthat accepts a String namedmessageas 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