WorkflowRuntime.GetService Method

Definition

Overloads

GetService(Type)

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

GetService<T>()

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

GetService(Type)

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

C#
public object GetService(Type serviceType);

Parameters

serviceType
Type

The Type of the service to retrieve.

Returns

The service of the specified Type.

Implements

Exceptions

serviceType is a null reference (Nothing in Visual Basic).

The WorkflowRuntime is already disposed of.

More than one service of type serviceType was found.

Remarks

GetService throws an InvalidOperationException if more than one service exists for the specified Type. Therefore, you should use one of the overloaded methods of GetAllServices if it is possible that multiple services of the specified 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.

Applies to

.NET Framework 4.8.1 和其他版本
产品 版本
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

GetService<T>()

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

C#
public T GetService<T>();

Type Parameters

T

The service type.

Returns

T

A single service of the specified generic type.

Exceptions

The WorkflowRuntime has already been disposed of.

More than one service of the generic type was found.

Examples

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.

C#
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()))
        {
            CompositeActivityDesigner.InsertActivities(this,
                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();
            }
        }
    }
}

Remarks

GetService 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.

Applies to

.NET Framework 4.8.1 和其他版本
产品 版本
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1