WorkflowRuntime.AddService(Object) 方法

定義

加入指定的服務至工作流程執行階段引擎。

public:
 void AddService(System::Object ^ service);
public void AddService (object service);
member this.AddService : obj -> unit
Public Sub AddService (service As Object)

參數

service
Object

物件,表示要新增的服務。

例外狀況

service 為 null 參考 (在 Visual Basic 中為 Nothing)。

service 已向工作流程執行階段引擎註冊。

-或- service 是核心服務,而且工作流程執行階段引擎已經在執行中 (IsStartedtrue)。

範例

下列程式碼範例示範如何從工作流程主機使用 WorkflowRuntime 功能。 提供如何使用 AddService 方法,將 ExternalDataExchangeServiceSqlWorkflowPersistenceService 加入工作流程執行階段引擎的範例。

此程式碼範例是 取消工作流程 範例的一部分。

static void Main()
{
    string connectionString = "Initial Catalog=SqlPersistenceService;Data Source=localhost;Integrated Security=SSPI;";

    using (WorkflowRuntime workflowRuntime = new WorkflowRuntime())
    {
        ExternalDataExchangeService dataService = new ExternalDataExchangeService();
        workflowRuntime.AddService(dataService);
        dataService.AddService(expenseService);

        workflowRuntime.AddService(new SqlWorkflowPersistenceService(connectionString));
        workflowRuntime.StartRuntime();

        workflowRuntime.WorkflowCompleted += OnWorkflowCompleted;
        workflowRuntime.WorkflowTerminated += OnWorkflowTerminated;
        workflowRuntime.WorkflowIdled += OnWorkflowIdled;
        workflowRuntime.WorkflowAborted += OnWorkflowAborted;

        Type type = typeof(SampleWorkflow1);
        WorkflowInstance workflowInstance = workflowRuntime.CreateWorkflow(type);
        workflowInstance.Start();

        waitHandle.WaitOne();

        workflowRuntime.StopRuntime();
    }
}
Shared Sub Main()
    Dim connectionString As String = "Initial Catalog=SqlPersistenceService;Data Source=localhost;Integrated Security=SSPI;"
    Using workflowRuntime As New WorkflowRuntime()
        Dim dataService As New ExternalDataExchangeService()
        workflowRuntime.AddService(dataService)
        dataService.AddService(expenseService)

        workflowRuntime.AddService(New SqlWorkflowPersistenceService(connectionString))


        AddHandler workflowRuntime.WorkflowCompleted, AddressOf OnWorkflowCompleted
        AddHandler workflowRuntime.WorkflowTerminated, AddressOf OnWorkflowTerminated
        AddHandler workflowRuntime.WorkflowIdled, AddressOf OnWorkflowIdled
        AddHandler workflowRuntime.WorkflowAborted, AddressOf OnWorkflowAborted


        Dim workflowInstance As WorkflowInstance
        workflowInstance = workflowRuntime.CreateWorkflow(GetType(SampleWorkflow))
        workflowInstance.Start()

        waitHandle.WaitOne()

        workflowRuntime.StopRuntime()
    End Using
End Sub

備註

您可以透過加入核心服務來設定工作流程執行階段引擎。 核心服務是衍生自下列任一服務基底類別 (Base Class) 的服務:WorkflowSchedulerService 類別、WorkflowCommitWorkBatchService 類別、WorkflowPersistenceService 類別和 TrackingService 類別。 只有在工作流程執行時間引擎未執行時,才能新增核心服務;也就是說,當 為 falseIsStartedWorkflowRuntime 也可以當做其他服務的儲存容器使用,其他工作流程或主機上執行的應用程式也可以使用。 如果您在工作流程執行階段引擎啟動後,加入衍生自 WorkflowRuntimeService 類別的非核心服務,則 AddService 會呼叫該服務實作的 Start 方法。

注意

AddService 會強制限制不能有兩個屬於相同 Type 的服務加入至 WorkflowRuntime。 然而,您可以加入衍生自相同基底類別的多個服務。 WorkflowRuntime 中,只能加入各一個衍生自下列服務基底類別的服務:WorkflowSchedulerService 類別、WorkflowCommitWorkBatchService 類別和 WorkflowPersistenceService 類別。 如果加入多個衍生自這些類別其中之一的服務 (例如兩個持續性服務),StartRuntime 會擲回 InvalidOperationException

適用於