共用方式為


HOW TO:新增和移除工作流程服務

您可以在工作流程執行階段中新增和移除服務。 您可以使用在 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 執行階段工作 2:使用 App.Config 設定執行階段服務Workflow Configuration Formats

請參閱

參考

WorkflowCommitWorkBatchService
WorkflowPersistenceService
TrackingService
WorkflowSchedulerService
WorkflowLoaderService

概念

建立工作流程主應用程式

其他資源

開發已啟用工作流程的應用程式

Footer image

Copyright © 2007 by Microsoft Corporation. All rights reserved.