Edit

WorkflowControlActions class

Namespace: Microsoft.Azure.Workflows.Sdk

Provides factory methods for creating control flow actions that branch, group, repeat, or stop execution. Access this class through WorkflowBuiltInActions by using WorkflowActions.BuiltIn.Control.

Usage

// Access control actions via WorkflowActions.BuiltIn.Control
var trigger = WorkflowTriggers.BuiltIn.CreateHttpTrigger();

var scope = WorkflowActions.BuiltIn.Control.Scope(() =>
{
    var step1 = WorkflowActions.BuiltIn.Compose(inputs: () => "Process").WithName("Process");
    return step1;
}).WithName("ProcessingScope");

trigger.Then(scope);
WorkflowFactory.CreateStatefulWorkflow("ControlFlowExample", trigger);

Methods

Scope

Creates a Scope action that groups nested actions into a single IWorkflowAction.

IWorkflowAction Scope(Func<IChainableNode> actions)
Name Description Type Required
actions Factory that builds the nested action graph. Func<IChainableNode> Yes
var scope = WorkflowActions.BuiltIn.Control.Scope(() =>
{
    var step1 = WorkflowActions.BuiltIn.Compose(inputs: () => "Step 1").WithName("Step1");
    var step2 = WorkflowActions.BuiltIn.Compose(inputs: () => "Step 2").WithName("Step2");
    return step1.Then(step2);
}).WithName("MyScope");

Condition

Creates a conditional action that evaluates a Boolean expression and routes execution to one of two branches.

IWorkflowAction Condition(Expression<Func<bool>> expression, Func<IChainableNode> trueBranch, Func<IChainableNode> falseBranch)
Name Description Type Required
expression Boolean expression to evaluate. Expression<Func<bool>> Yes
trueBranch Factory for the true branch actions. Func<IChainableNode> Yes
falseBranch Factory for the false branch actions. Func<IChainableNode> Yes
var condition = WorkflowActions.BuiltIn.Control.Condition(
    expression: () => trigger.TriggerOutput.Body != null,
    trueBranch: () => WorkflowActions.BuiltIn.Compose(inputs: () => "Has body").WithName("True"),
    falseBranch: () => WorkflowActions.BuiltIn.Compose(inputs: () => "No body").WithName("False")
).WithName("CheckBody");

ForEach

Creates a loop action that iterates over a collection and executes the supplied action graph for each item.

IWorkflowAction ForEach(Expression<Func<JToken>> items, Func<JToken, IChainableNode> actions)
Name Description Type Required
items Expression for the collection to iterate. Expression<Func<JToken>> Yes
actions Factory receiving the current item and returning an action graph. Func<JToken, IChainableNode> Yes
var forEach = WorkflowActions.BuiltIn.Control.ForEach(
    items: () => JArray.Parse("[\"one\",\"two\"]"),
    actions: (item) => WorkflowActions.BuiltIn.Compose(inputs: () => $"Item: {item}").WithName("ProcessItem")
).WithName("LoopItems");

Until

Creates a loop action that repeats its nested actions until the supplied expression evaluates to true.

IWorkflowAction Until(Expression<Func<bool>> expression, Func<IChainableNode> actions)
Name Description Type Required
expression Boolean exit condition. Expression<Func<bool>> Yes
actions Factory that builds the repeated action graph. Func<IChainableNode> Yes
var counterVar = WorkflowActions.BuiltIn.Variables.InitializeVariable<int>(
    name: () => "counter",
    value: () => 0).WithName("Counter");

var until = WorkflowActions.BuiltIn.Control.Until(
    expression: () => counterVar.Value.Value<int>() >= 10,
    actions: () => WorkflowActions.BuiltIn.Variables.IncrementVariable<int>(
        name: () => "counter", value: () => 1).WithName("Increment")
).WithName("RepeatUntil10");

Switch

Creates a switch action that evaluates an input and routes execution to a matching SwitchCase or a default branch.

IWorkflowAction Switch(Expression<Func<JToken>> on, Func<Dictionary<string, SwitchCase>> cases, Func<IChainableNode> defaultCase = null)
Name Description Type Required
on Expression for the value to switch on. Expression<Func<JToken>> Yes
cases Factory returning case label to SwitchCase mapping. Func<Dictionary<string, SwitchCase>> Yes
defaultCase Factory for the default case actions. Func<IChainableNode> No
var switchAction = WorkflowActions.BuiltIn.Control.Switch(
    on: () => trigger.TriggerOutput.Body,
    cases: () => new Dictionary<string, SwitchCase>
    {
        ["case1"] = new SwitchCase("value1",
            WorkflowActions.BuiltIn.Compose(inputs: () => "Case 1").WithName("HandleCase1")),
        ["case2"] = new SwitchCase("value2",
            WorkflowActions.BuiltIn.Compose(inputs: () => "Case 2").WithName("HandleCase2"))
    },
    defaultCase: () => WorkflowActions.BuiltIn.Compose(inputs: () => "Default").WithName("HandleDefault")
).WithName("RouteByValue");

Terminate

Creates a terminate action that stops the workflow with a specified FlowStatus and optional message.

IWorkflowAction Terminate(Expression<Func<FlowStatus>> status, Expression<Func<string>> message = null)
Name Description Type Required
status Expression for termination status. Expression<Func<FlowStatus>> Yes
message Expression for termination message. Expression<Func<string>> No
var terminate = WorkflowActions.BuiltIn.Control.Terminate(
    status: () => FlowStatus.Failed,
    message: () => "Critical error occurred").WithName("StopWorkflow");