How Workflows Handle Events from the Host Process?

WF uses the same infrastructure for handling events as it does for calling methods. It handles events in the following way:

  1. The exchange service interface defines the events in the host process that are accessible to the workflow.

  2. The workflow subscribes to events in the exchange service through the workflow runtime engine.

  3. When events are raised on the host process, the exchange service notifies the workflow runtime, which passes the event information to the workflow as an inbound message.

  4. The workflow runtime event handler executes.

When workflows handle events, data passes asynchronously between the host and workflow processes, unlike when calling methods on the host process, where data passes synchronously.

Workflows subscribe to events that are defined in the exchange service by using a HandleExternalEventActivity activity. You use the Invoked property in the activity to specify a method to execute when the host process raises the event. When the event is raised in the host process, the service raises the corresponding event, which the workflow runtime engine intercepts and passes to the workflow as an inbound message. The workflow then executes the functionality that you have defined for the HandleExternalEventActivity activity.

For more information about handling events on the host process, see Using Local Services in Workflows.

 

Event Handling in a Workflow

There are several ways to handle events in WF workflows. The table below describes two event-handling activities, ListenActivity and HandleExternalEventActivity, and some of the common parameters that must be configured to handle the event.

Item

Usage

ListenActivity

Use ListenActivity to listen for events. ListenActivity is a wrapper for several EventDrivenActivity activities, each of which enables you to handle an external event.

The first child activity in any EventDrivenActivity activity must implement a System.Workflow.Activities.IEventActivity activitysuch as DelayActivity or HandleExternalEventActivity.

HandleExternalEventActivity

Use a stand-alone HandleExternalEventActivity activity to wait for a specific event to occur. Workflow execution halts until the event is raised. To include a time-out in case the event is not raised, implement a ListenActivity activity with your expected event in one branch and a DelayActivity activity that is set to the length of the time-out that you require in another.

ParameterBindings

Use the ParameterBindings object to pass data between the host process and the event handlers, in the same way that you call methods on the host process.

InterfaceType

Specify the InterfaceType property of the activity to subscribe to events.

When you select the InterfaceType property, you can open the Browse and Select a .NET type dialog box, which enables you to browse the referenced assemblies and select the one that contains your interface. You then select the correct interface from the list.

When you specify the InterfaceType property, the EventName list is populated so that you can select the correct event to handle.

EventName

Specify the EventName property of the activity to subscribe to events.

For more information about the ListenActivity activity, see Using the ListenActivity Activity.

For more information about the HandleExternalEventActivity class, see HandleExternalEventActivity Class.