WorkflowRuntime.GetService<T> Method ()


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Retrieves a service of the specified generic type from the workflow run-time engine.

Namespace:   System.Workflow.Runtime
Assembly:  System.Workflow.Runtime (in System.Workflow.Runtime.dll)

public T GetService<T>()

Return Value

Type: T

A single service of the specified generic type.

Type Parameters


The service type.

Exception Condition

The WorkflowRuntime has already been disposed of.


More than one service of the generic type was found.

GetService<T> throws an InvalidOperationException if more than one service exists for the specified generic type. Therefore, you should use one of the overloaded methods of GetAllServices if it is possible that multiple services of the generic type are present in the WorkflowRuntime. For example, the workflow run-time engine may have multiple tracking services. If you request a tracking service by specifying the TrackingService base class, it is possible that an exception will be thrown.

The following example demonstrates how to retrieve a single service from a WorkflowRuntime object; in this case, a service of type ManualWorkflowSchedulerService. This example is from the Workflow Threading Sample.

protected override CompositeActivity OnCreateNewBranch()
    return new ParallelIfBranch();

private void OnAddBranch(object sender, EventArgs e)
    CompositeActivity activity1 = this.OnCreateNewBranch();
    CompositeActivity activity2 = base.Activity as CompositeActivity;

    if ((activity2 != null) && (activity1 != null))
        int num1 = this.ContainedDesigners.Count;
        Activity[] activityArray1 = new Activity[] { activity1 };

        if (CanInsertActivities(new ConnectorHitTestInfo(this, HitTestLocations.Designer, activity2.Activities.Count),
            new List<Activity>(activityArray1).AsReadOnly()))
                new ConnectorHitTestInfo(this, HitTestLocations.Designer, activity2.Activities.Count),
                new List<Activity>(activityArray1).AsReadOnly(),
                string.Format("Adding branch {0}", activity1.GetType().Name));

            if ((this.ContainedDesigners.Count > num1) && (this.ContainedDesigners.Count > 0))
                this.ContainedDesigners[this.ContainedDesigners.Count - 1].EnsureVisible();

.NET Framework
Available since 3.0
Return to top