How to Call Methods on the Host Process?
Windows Workflow Foundation (WF) enables you to integrate your workflow solutions seamlessly into applications that are based on the Windows operating system. The ability of the workflow to call methods in the host process and then to handle events from the host process to control execution flow is functionality that is often required. It is also sometimes necessary to enable workflows to consume Windows Communication Foundation (WCF) services or to publish a workflow as a WCF service so that other applications can easily consume it. WF supports all of these scenarios.
The trend in modern programming is toward distributed applications. One of the main features of distributed applications is their ability to communicate with other applications and processes to enable separate parts of an application to work together to perform the required functionality. WF workflows are no exception to this rule; you can develop workflow applications to work as part of a much larger system or configure a workflow to retrieve data from other processes as necessary. The inclusion of SendActivity and ReceiveActivity in WF 3.5 provides the functionality to call methods on external processes and handle events from external applications. This integration of WF and WCF enables you to implement workflow calls into WCF services and to embed workflows into a service to take advantage of the benefits of a WF application in the cross-platform, language-independent environment that a WCF service provides
How to Call Methods on the Host Process?
WF provides the functionality that enables you to call methods on the host process from within a workflow. You use CallExternalMethodActivity to specify the interface type and method name that you want to call on the host process
Why Call Methods on the Host Process?
There are several scenarios during the execution of your workflow where you must call methods on the host process. Although it is possible for you to use CodeActivity and implement the cross-process call by using code in the Microsoft Visual Basic developmen system or the Microsoft Visual C# development tool, WF provides CallExternalMethodActivity to provide the communication logic for you.
One common reason to call host methods is to cause a change in the user interface in response to workflow status or changes to variables. In most software development scenarios, it is good practice to keep interface code separate from the business logic. Therefore, it is necessary to implement cross-process method calls to update the user interface. CallExternalMethodActivity enables this functionality in the workflow context.
Another scenario arises when the workflow requires data from a database. If the system that you are writing utilizes a data access layer to manage communication between the business logic and the database, you must call methods on the access layer to retrieve data that the workflow can use. Again, CallExternalMethodActivity enables you to call the data access layer and accept any data that is passed back from the method call.
How Workflows Call Methods on the Host Process
WF supports communication in the workflow hosting environment by using local communication services. Workflow communication services enable objects to interact with a workflow instance.
The workflow treats the communication service as a channel that contains inbound events and outbound method invocations. The service converts method declarations in the interface to method calls on the service. Data passes between the workflow and host method by using custom event argument collections.
WF supports communication in the workflow hosting environment by using local communication services, as follows:
- The workflow uses CallExternalMethodActivity to make a call to the ExternalDataExchange service.
- When the ExternalDataExchange service receives a call from the workflow, it raises the relevant event, which is handled by the destination method in the host application.
- The host service method executes. Any data that is returned by the method passes to the workflow instance that is called the ExternalDataExchange service
Calling Methods from the Workflow
There are three things that you must do to enable your workflow to communicate with the host process: create the communication service, expose methods from the host process, and enable the workflow to call into the communication service.
You define the communication service as an interface, which is passed to the service runtime to facilitate communication. The service is annotated with the ExternalDataExchangeattribute. Your communication service implementation is instantiated by the workflow runtime engine and runs in the same process.
To make host process methods available to workflows:
- Define an interface that specifies available methods.
- Specify the ExternalDataExchange attribute on the interface.
- Define a service class that implements the interface.
- Register a service class instance with the workflow runtime engine.
To call a host process method from a workflow:
- Add a CallExternalMethodActivity to the workflow.
- Specify the InterfaceType and MethodName properties to specify which method in which interface you want to call.
The following code examples show the creation of the service interface, ICommunicationService, which exposes a single method. The code examples then create an object that implements the interface. The final lines of the code examples add the service to the workflow runtime, which makes it available to the workflow.
For more information about calling methods on the host process, see Using Local Services in Workflows.
For more information about the ExternalDataExchangeService class, see ExternalDataExchangeService Class.
For more information about the CallExternalMethodActivity class, see CallExternalMethodActivity Class