如何:添加和移除工作流服务

可以添加和移除工作流运行时中的服务。 可以使用在 WorkflowRuntime 类中定义的 AddService 方法添加服务。 使用同样在 WorkflowRuntime 类中定义的 RemoveService 方法移除服务。

向工作流运行时引擎添加服务

通过使用在 WorkflowRuntime 类中定义的 StartRuntime 方法开始执行工作流引擎之前,可以添加工作流所需的服务。 若要添加新服务,请创建服务对象的新实例,并调用在 WorkflowRuntime 类中定义的 AddService 方法,并传递服务对象作为参数。 下面的示例演示如何创建和启动运行时引擎、创建创建自定义跟踪服务中所述 EventLogTrackingService 服务的实例以及将该服务添加到工作流运行时引擎。

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();
        }
    }
}

从工作流运行时引擎移除服务

如同可以向运行时引擎添加服务一样,也可以移除这些服务。 为此,要调用在 WorkflowRuntime 类中定义的 RemoveService 方法。 此方法的参数是要移除的服务的实例。 只有在工作流运行时引擎未运行的情况下才能移除服务。 因此,必须首先调用 StopRuntime 方法,移除服务,然后通过调用 StartRuntime 再次启动运行时。 下面的示例通过移除 EventLogTrackingService 服务对较早显示的示例进行了扩展。

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);
        }
    }

备注

运行时引擎在运行过程中,无法移除运行时引擎自身所创建的任何服务。 运行时开始执行时就添加了这些默认服务,管理任何工作流的执行都需要这些服务。

运行时引擎在运行过程中,无法移除以下服务以及从这些类型派生的服务:

有关如何通过使用配置文件添加和移除服务的信息,请参见位于教程:承载 Windows Workflow Foundation 运行库Workflow Configuration Formats中的任务 2:使用 App.Config 配置运行时服务

请参见

参考

WorkflowCommitWorkBatchService
WorkflowPersistenceService
TrackingService
WorkflowSchedulerService
WorkflowLoaderService

概念

创建工作流宿主应用程序

其他资源

开发启用工作流的应用程序

Footer image

版权所有 (C) 2007 Microsoft Corporation。保留所有权利。