Metodo AddService

Metodo WorkflowRuntime.AddService

Adds the specified service to the workflow runtime engine.

Spazio dei nomi: System.Workflow.Runtime
Assembly: System.Workflow.Runtime (in system.workflow.runtime.dll)

public void AddService (
	Object service
public void AddService (
	Object service
public function AddService (
	service : Object
Non applicabile.



An object that represents the service to add.

Tipo di eccezioneCondizione


service is a null reference (Nothing in Visual Basic)


The WorkflowRuntime is disposed.


service is already registered with the workflow runtime engine.


service is a core service and the workflow runtime engine is already running (IsStarted is true).

You can configure the workflow runtime engine by adding core services. Core services are those that derive from any of the following service base classes: the WorkflowSchedulerService class, the WorkflowCommitWorkBatchService class, the WorkflowPersistenceService class, and the TrackingService class. Core services can only be added when the workflow runtime engine is not running; that is, when IsStarted is false. The WorkflowRuntime can also be used as a storage container for other services that may be used by other workflows or by applications running on a host. If you add a non-core service that derives from the WorkflowRuntimeService class after the workflow runtime engine has been started, AddService will call the Start method implemented by that service.


 AddService enforces the restriction that no two services of the same Type may be added to the WorkflowRuntime; however, you can add multiple services that derive from the same base class. There can be only one service derived from each of the following service base classes in the WorkflowRuntime: the WorkflowSchedulerService class, the WorkflowCommitWorkBatchService class, and the WorkflowPersistenceService class. If you add multiple services derived from one of these classes, for example two persistence services, StartRuntime will throw an InvalidOperationException.

The following code example demonstrates how you can use WorkflowRuntime functionality from a workflow host. It provides example of how to use the AddService method to add an ExternalDataExchangeService and SqlWorkflowPersistenceService to the workflow runtime engine.

This code example is part of the Cancelling a Workflow SDK sample from the Program.cs file. For more information, see Cancelling a Workflow.

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(new SqlWorkflowPersistenceService(connectionString));

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

        Type type = typeof(Microsoft.Samples.Workflow.CancelWorkflow.SampleWorkflow);
        WorkflowInstance workflowInstance = workflowRuntime.CreateWorkflow(type);



Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile per Pocket PC, Windows Mobile per Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

Microsoft .NET Framework 3.0 è supportato in Windows Vista, Microsoft Windows XP SP2 e Windows Server 2003 SP1.

.NET Framework

Supportato in:

Aggiunte alla community

© 2015 Microsoft