Export (0) Print
Expand All

WorkflowInvoker.Invoke Method (Activity, IDictionary<String, Object>)

Invokes a workflow synchronously using the specified workflow definition and IDictionary<TKey, TValue> of input parameters.

Namespace:  System.Activities
Assembly:  System.Activities (in System.Activities.dll)

public static IDictionary<string, Object> Invoke(
	Activity workflow,
	IDictionary<string, Object> inputs
)

Parameters

workflow
Type: System.Activities.Activity

The workflow definition of the workflow to invoke.

inputs
Type: System.Collections.Generic.IDictionary<String, Object>

The dictionary of input parameters to the workflow, keyed by argument name.

Return Value

Type: System.Collections.Generic.IDictionary<String, Object>
A dictionary of the root activity’s OutArgument and InOutArgument values keyed by argument name that represent the outputs of the workflow.

This method blocks until the workflow has completed, including idle time. All workflow execution is guaranteed to execute on the invoking thread. To configure a time-out interval in which the workflow must complete, use one of the Invoke overloads that take a TimeSpan.

The following example invokes a workflow consisting of a single Divide activity that has two input arguments and two output arguments. When the workflow is invoked, the arguments dictionary is passed which contains the values for each input argument, keyed by argument name. When the call to Invoke returns, each output argument is returned in the outputs dictionary, also keyed by argument name.

public sealed class Divide : CodeActivity
{
    [RequiredArgument]
    public InArgument<int> Dividend { get; set; }

    [RequiredArgument]
    public InArgument<int> Divisor { get; set; }

    public OutArgument<int> Remainder { get; set; }
    public OutArgument<int> Result { get; set; }

    protected override void Execute(CodeActivityContext context)
    {
        int quotient = Dividend.Get(context) / Divisor.Get(context);
        int remainder = Dividend.Get(context) % Divisor.Get(context);

        Result.Set(context, quotient);
        Remainder.Set(context, remainder);
    }
}
int dividend = 500;
int divisor = 36;

Dictionary<string, object> arguments = new Dictionary<string, object>();
arguments.Add("Dividend", dividend);
arguments.Add("Divisor", divisor);

IDictionary<string, object> outputs =
    WorkflowInvoker.Invoke(new Divide(), arguments);

Console.WriteLine("{0} / {1} = {2} Remainder {3}",
    dividend, divisor, outputs["Result"], outputs["Remainder"]);

If the workflow derives from ActivityWithResult, such as CodeActivity<TResult> or Activity<TResult>, and there are output arguments in addition to the well-defined Result output argument, a non-generic overload of Invoke, such as this one, must be used in order to retrieve the additional arguments. To do this, the workflow definition passed into Invoke must be of type Activity. In this example the Divide activity derives from CodeActivity<int>, but is declared as Activity so that this overload of Invoke, which returns a dictionary of arguments instead of a single return value, is used.

public sealed class Divide : CodeActivity<int>
{
    public InArgument<int> Dividend { get; set; }
    public InArgument<int> Divisor { get; set; }
    public OutArgument<int> Remainder { get; set; }

    protected override int Execute(CodeActivityContext context)
    {
        int quotient = Dividend.Get(context) / Divisor.Get(context);
        int remainder = Dividend.Get(context) % Divisor.Get(context);

        Remainder.Set(context, remainder);

        return quotient;
    }
}
int dividend = 500;
int divisor = 36;

Dictionary<string, object> arguments = new Dictionary<string, object>();
arguments.Add("Dividend", dividend);
arguments.Add("Divisor", divisor);

Activity wf = new Divide();

IDictionary<string, object> outputs =
    WorkflowInvoker.Invoke(wf, arguments);

Console.WriteLine("{0} / {1} = {2} Remainder {3}",
    dividend, divisor, outputs["Result"], outputs["Remainder"]);

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4

.NET Framework Client Profile

Supported in: 4

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft