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.

public:
 virtual System::Object ^ GetService(Type ^ serviceType);
public object GetService (Type serviceType);
abstract member GetService : Type -> obj
override this.GetService : Type -> obj
Public Function GetService (serviceType As Type) As Object

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

GetService<T>()

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

public:
generic <typename T>
 T GetService();
public T GetService<T> ();
member this.GetService : unit -> 'T
Public Function GetService(Of T) () As 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.

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();
            }
        }
    }
}
Protected Overrides Function OnCreateNewBranch() As CompositeActivity
    Return New ParallelIfBranch()
End Function

Private Sub OnAddBranch(ByVal sender As Object, ByVal e As EventArgs)
    Dim activity1 As CompositeActivity = Me.OnCreateNewBranch()
    Dim activity2 As CompositeActivity = CType(MyBase.Activity, CompositeActivity)

    If (activity2 IsNot Nothing) And (activity1 IsNot Nothing) Then

        Dim num1 As Integer = Me.ContainedDesigners.Count
        Dim activityArray1() As Activity = {activity1}

        If (CanInsertActivities(New ConnectorHitTestInfo(Me, HitTestLocations.Designer, activity2.Activities.Count), _
            New List(Of Activity)(activityArray1).AsReadOnly())) Then

            CompositeActivityDesigner.InsertActivities(Me, _
                New ConnectorHitTestInfo(Me, HitTestLocations.Designer, activity2.Activities.Count), _
                New List(Of Activity)(activityArray1).AsReadOnly(), _
                String.Format("Adding branch 0}", activity1.GetType().Name))

            If (Me.ContainedDesigners.Count > num1) And (Me.ContainedDesigners.Count > 0) Then
                Me.ContainedDesigners(Me.ContainedDesigners.Count - 1).EnsureVisible()
            End If
        End If
    End If
End Sub

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