How to: Add and Remove Workflow Services

You can add and remove services in the workflow runtime. You can add services by using the AddService method that is defined in the WorkflowRuntime class. You remove services by using the RemoveService method, also defined in the WorkflowRuntime class.

Adding Services to the Workflow Runtime Engine

Before the workflow engine begins execution by using the StartRuntime method that is defined in the WorkflowRuntime class, you can add the services that are required for your workflow. To add a new service, create a new instance of your service object and call the AddService method that is defined in the WorkflowRuntime class, passing your service object as a parameter. The following example demonstrates how to create and start the runtime engine, create an instance of the EventLogTrackingService service described in Creating Custom Tracking Services, and add that service to the workflow runtime engine.

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Workflow.Runtime;
using System.Workflow.Runtime.Hosting;

namespace WorkflowApplication
{
    class WorkflowHostExample
    {
        static void Main(string[] args)
        {
            // Create the runtime and EventLogTrackingService objects.
            WorkflowRuntime runtime = new WorkflowRuntime();
            EventLogTrackingService eventlogService = new EventLogTrackingService();

            // Add the EventLogTrackingService and start the runtime. 
            runtime.AddService(eventlogService);
            runtime.StartRuntime();

            ...

            runtime.StopRuntime();
        }
    }
}

Removing Services from the Workflow Runtime Engine

Just as you can add services to the runtime engine, you can also remove them. You do this by calling the RemoveService method that is defined in the WorkflowRuntime class. The parameter to this method is the instance of the service you want to remove. A service can be removed only when the workflow runtime engine is not running. You must therefore call the StopRuntime method first, remove the service, and then start the runtime again by calling StartRuntime. The following example expands on the example shown earlier by removing the EventLogTrackingService service.

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Workflow.Runtime;
using System.Workflow.Runtime.Hosting;

namespace WorkflowApplication
{
    class WorkflowHostExample
    {
        static void Main(string[] args)
        {
            // Create the runtime and EventLogTrackingService objects.
            WorkflowRuntime runtime = new WorkflowRuntime();
            EventLogTrackingService eventlogService = new EventLogTrackingService();

            // Add the EventLogTrackingService and start the runtime.
            runtime.AddService(eventlogService);
            runtime.StartRuntime();

            ...
            // Stop the runtime and remove the EventLogTrackingService.
            runtime.StopRuntime();
            runtime.RemoveService(eventlogService);
        }
    }

Note

You cannot remove any services that were created by the runtime engine itself while the runtime engine is running. These default services are added when the runtime begins executing and are needed to manage the execution of any workflows.

The following services, and those that derive from these types, cannot be removed while the runtime engine is running:

For information about how to add and remove services by using a configuration file, see Task 2: Configure Runtime Services using App.Config, located in Tutorial: Host the WF Runtime, and Workflow Configuration Formats.

See Also

Reference

WorkflowCommitWorkBatchService
WorkflowPersistenceService
TrackingService
WorkflowSchedulerService
WorkflowLoaderService

Concepts

Creating a Workflow Host Application

Other Resources

Developing Workflow-Enabled Applications