Classes, Methods, and Events of a Runtime Service in Windows Workflow (WF)

To create a custom runtime service, define a class that inherits directly or indirectly from WorkflowRuntimeService and then override methods from the base class. The WorkflowRuntimeService class has a property that is named Runtime that provides access to the workflow runtime engine that hosts the service. You can use the object that the Runtime property returns to view and modify the workflows and services that are running in the workflow runtime engine.

When you define a runtime service class, you typically override the methods in the following table from the WorkflowRuntimeService class.

 

WorkflowRuntimeService Method

Description

OnStarted

This method indicates that the service has started. Override this method to perform any initialization that the service requires.

OnStopped

This method indicates that the service has stopped. Override this method to perform any tidying by the service.

 

The WorkflowRuntimeService class has a Runtime property that provides access to the workflows and services that are running in the workflow runtime engine. This enables you to create new workflows and services, if required. The following table describes some of the key methods that are available on the Runtime object.

 

Runtime Method

Description

StartRuntime

This method starts the workflow runtime engine.

StopRuntime

This method stops the workflow runtime engine.

AddService

This method adds a service to the workflow runtime engine.

RemoveService

This method removes a service from the workflow runtime engine.

GetAllServices

This method gets all of the services that have been added to the workflow runtime engine.

GetService

This method gets a service that has a specified data type.

CreateWorkflow

This method creates a new workflow instance.

GetLoadedWorkflows

This method gets all of the workflow instances that are currently loaded in the workflow runtime engine.

GetWorkflow

This method gets a workflow instance that has a specified instanceID property value.

 

The object that the Runtime property returns defines several events that you can handle in a service class. The following table describes some of the key events.

 

Runtime Event

Description

WorkflowCreated

This event indicates that a workflow has been created.

WorkflowStarted

This event indicates that a workflow has started.

WorkflowCompleted

This event indicates that a workflow has completed.

WorkflowTerminated

This event indicates that a workflow has terminated.

 

The following code examples show how to define a custom service class that is named MyService that inherits directly from WorkflowRuntimeService. The MyService class overrides the OnStarted and OnStopped methods from the base class as follows:

·         OnStarted invokes the base-class version of the method, displays a console message, and then adds event-handler methods for the WorkflowStarted, WorkflowCompleted, and WorkflowTerminated events on the Runtime object.

·         OnStopped invokes the base-class version of the method and then displays a console message.

The MyService class also contains local event-handler methods that are named Started, Completed, and Terminated to handle the WorkflowStarted, WorkflowCompleted, and WorkflowTerminated events. These methods display messages to indicate the instance ID of the workflow that has started, completed, or terminated.

 

[Visual Basic]

Public Class MyService

    Inherits WorkflowRuntimeService

 

    Protected Overrides Sub OnStarted()

        MyBase.OnStarted()

        Console.WriteLine("My service has started.")

 

        AddHandler Runtime.WorkflowStarted, AddressOf Started

        AddHandler Runtime.WorkflowCompleted, AddressOf Completed

        AddHandler Runtime.WorkflowTerminated, AddressOf Terminated

    End Sub

 

    Protected Overrides Sub OnStopped()

        MyBase.OnStopped()

Tags: