static void Main(string[] args) { // iteration variable for the ForEach var item = new DelegateInArgument<object>(); // list of elements to iterate ArrayList list = new ArrayList(); list.Add("Bill"); list.Add("Steve"); list.Add("Ray"); // iterate through the list and show the elements Activity act = new ForEach { Values = new InArgument<IEnumerable>(ctx=>list), Body = new ActivityAction<object> { Argument = item, Handler = new WriteLine { Text = new InArgument<string>(c => item.Get(c).ToString()) } } }; WorkflowInvoker.Invoke(act); Console.WriteLine(""); Console.WriteLine("Press enter to exit..."); Console.ReadLine(); }
// Creates a workflow that uses FindInSqlTable activity with a more complex predicate to retrieve entities from a Sql Server Database static Activity CreateRetrieveWithComplexPredicateWF() { Variable<IList<Employee>> employees = new Variable<IList<Employee>>(); DelegateInArgument<Employee> employeeIterationVariable = new DelegateInArgument<Employee>(); return new Sequence() { Variables = { employees }, Activities = { new WriteLine { Text = "\r\nEmployees by Role and Location (more complex predicate)\r\n============================================" }, new FindInSqlTable<Employee> { ConnectionString = cnn, Predicate = new LambdaValue<Func<Employee, bool>>(c => new Func<Employee, bool>(emp => emp.Role.Equals("PM") && emp.Location.Equals("Redmond"))), Result = new OutArgument<IList<Employee>>(employees) }, new ForEach<Employee> { Values = new InArgument<IEnumerable<Employee>>(employees), Body = new ActivityAction<Employee>() { Argument = employeeIterationVariable, Handler = new WriteLine { Text = new InArgument<string>(env => employeeIterationVariable.Get(env).ToString()) } } } } }; }
/// <summary> /// Create internal states /// </summary> public static void ProcessChildStates(NativeActivityMetadata metadata, Collection<State> childStates, Collection<InternalState> internalStates, Collection<ActivityFunc<string, StateMachineEventManager, string>> internalStateFuncs) { foreach (State state in childStates) { InternalState internalState = state.InternalState; internalStates.Add(internalState); DelegateInArgument<string> toStateId = new DelegateInArgument<string>(); DelegateInArgument<StateMachineEventManager> eventManager = new DelegateInArgument<StateMachineEventManager>(); internalState.ToState = toStateId; internalState.EventManager = eventManager; ActivityFunc<string, StateMachineEventManager, string> activityFunc = new ActivityFunc<string, StateMachineEventManager, string> { Argument1 = toStateId, Argument2 = eventManager, Handler = internalState, }; if (state.Reachable) { //If this state is not reached, we should not add it as child because it's even not well validated. metadata.AddDelegate(activityFunc); } internalStateFuncs.Add(activityFunc); } }
private static Activity CreatePrintProcessActivity(Variable<Collection<Process>> processes) { var proc = new DelegateInArgument<Process>("process"); return new Sequence { Activities = { // Loop over processes and print them out. new ForEach<Process> { Values = processes, Body = new ActivityAction<Process> { Argument = proc, Handler = new WriteLine { Text = new InArgument<string>(ctx => proc.Get(ctx).ToString()), } } }, // Print out a new line. new WriteLine(), } }; }
// Creates a workflow that uses FindInTable activity without predicate to retrieve entities from a Sql Server Database static Activity CreateRetrieveWithoutPredicateWF() { Variable<IList<Role>> roles = new Variable<IList<Role>>(); DelegateInArgument<Role> roleIterationVariable = new DelegateInArgument<Role>(); return new Sequence() { Variables = { roles }, Activities = { new WriteLine { Text = "\r\nAll Roles (no predicates)\r\n==============" }, new FindInSqlTable<Role> { ConnectionString = cnn, Result = new OutArgument<IList<Role>>(roles) }, new ForEach<Role> { Values = new InArgument<IEnumerable<Role>>(roles), Body = new ActivityAction<Role>() { Argument = roleIterationVariable , Handler = new WriteLine { Text = new InArgument<string>(env => roleIterationVariable.Get(env).Name) } } } } }; }
Constraint UnrootedRequestRule() { DelegateInArgument<SendReply> sendReply = new DelegateInArgument<SendReply>(); DelegateInArgument<ValidationContext> context = new DelegateInArgument<ValidationContext>(); DelegateInArgument<Activity> activityInTree = new DelegateInArgument<Activity>(); Variable<bool> requestInTree = new Variable<bool> { Default = false }; return new Constraint<SendReply> { Body = new ActivityAction<SendReply, ValidationContext> { Argument1 = sendReply, Argument2 = context, Handler = new Sequence { Variables = { requestInTree }, Activities = { new If { Condition = new InArgument<bool>(ctx => sendReply.Get(ctx).Request != null), Then = new Sequence { Activities = { new ForEach<Activity> { Values = new GetWorkflowTree { ValidationContext = context, }, Body = new ActivityAction<Activity> { Argument = activityInTree, Handler = new If { Condition = new InArgument<bool>(ctx => activityInTree.Get(ctx) == sendReply.Get(ctx).Request), Then = new Assign<bool> { To = requestInTree, Value = true, } } } }, new AssertValidation { Assertion = new InArgument<bool>(ctx => requestInTree.Get(ctx)), IsWarning = false, Message = new InArgument<string>(ctx => string.Format(CultureInfo.CurrentCulture, System.Activities.Core.Presentation.SR.UnrootedRequestInSendReply, sendReply.Get(ctx).DisplayName)) } } } } } } } }; }
Constraint<DialSipUri> MustBeInsideDialSipActivityConstraint() { var activityBeingValidated = new DelegateInArgument<DialSipUri>(); var validationContext = new DelegateInArgument<ValidationContext>(); var parent = new DelegateInArgument<Activity>(); var parentIsOuter = new Variable<bool>(); return new Constraint<DialSipUri>() { Body = new ActivityAction<DialSipUri, ValidationContext>() { Argument1 = activityBeingValidated, Argument2 = validationContext, Handler = new Sequence() { Variables = { parentIsOuter, }, Activities = { new ForEach<Activity>() { Values = new GetParentChain() { ValidationContext = validationContext, }, Body = new ActivityAction<Activity>() { Argument = parent, Handler = new If() { Condition = new InArgument<bool>(env => parent.Get(env).GetType() == typeof(DialSip)), Then = new Assign<bool>() { To = parentIsOuter, Value = true, }, }, }, }, new AssertValidation() { Assertion = parentIsOuter, Message = "DialSipUris must be nested inside DialSip", IsWarning = false, }, }, }, }, }; }
/// <summary> /// Creates the code for wrapping an activity. /// Returns a <see cref="System.Activities.Statements.TryCatch "/>activity that implements error handling logic /// </summary> /// <returns>The code for wrapping activity</returns> internal Activity CreateBody() { var exceptionArgument = new DelegateInArgument<Exception>(); return new TryCatch { Try = this.CreateInternalBody(), Catches = { new Catch<FailingBuildException> { Action = new ActivityAction<FailingBuildException> { Handler = new @If { Condition = new InArgument<bool>(env => this.IgnoreExceptions.Get(env)), Else = new Rethrow() } } }, new Catch<Exception> { Action = new ActivityAction<Exception> { Argument = exceptionArgument, Handler = new Sequence { Activities = { new @If { Condition = new InArgument<bool>(env => this.LogExceptionStack.Get(env)), Then = new LogBuildError { Message = new InArgument<string>(env => FormatLogExceptionStackMessage(exceptionArgument.Get(env))) } }, new @If { Condition = new InArgument<bool>(env => this.IgnoreExceptions.Get(env)), Else = new Rethrow() } } } } } } }; }
public static ActivityAction<Func<Task>> CreateActivityDelegate() { DelegateInArgument<Func<Task>> resultTaskFunc = new DelegateInArgument<Func<Task>>(); return new ActivityAction<Func<Task>>() { Argument = resultTaskFunc, Handler = new TaskFuncEvaluator() { ResultTaskFunc = resultTaskFunc, }, }; }
public static Constraint VerifyParentIsReceiveRequestSendResponseScope() { DelegateInArgument<Activity> element = new DelegateInArgument<Activity>(); DelegateInArgument<ValidationContext> context = new DelegateInArgument<ValidationContext>(); DelegateInArgument<Activity> parent = new DelegateInArgument<Activity>(); Variable<bool> result = new Variable<bool>(); return new Constraint<Activity> { Body = new ActivityAction<Activity, ValidationContext> { Argument1 = element, Argument2 = context, Handler = new Sequence { Variables = { result, }, Activities = { new ForEach<Activity> { Values = new GetParentChain { ValidationContext = context, }, Body = new ActivityAction<Activity> { Argument = parent, Handler = new If { Condition = new InArgument<bool>((env) => parent.Get(env).GetType() == typeof(ReceiveRequestSendResponseScope)), Then = new Assign<bool> { Value = true, To = result, }, }, }, }, new AssertValidation { Assertion = new InArgument<bool>((env) => result.Get(env)), Message = new InArgument<string>((env) => $"{element.Get(env).GetType().GetFriendlyName()} can only be added inside a {nameof(ReceiveRequestSendResponseScope)} activity."), PropertyName = new InArgument<string>((env) => element.Get(env).DisplayName), }, }, }, }, }; }
/// <summary> /// Validates whether the Twilio activity is contained within a CallScope activity. /// </summary> /// <returns></returns> internal static Constraint<Activity> MustBeInsideCallScopeConstraint() { var activityBeingValidated = new DelegateInArgument<Activity>(); var validationContext = new DelegateInArgument<ValidationContext>(); var parent = new DelegateInArgument<Activity>(); var parentIsCallScope = new Variable<bool>(); return new Constraint<Activity>() { Body = new ActivityAction<Activity, ValidationContext>() { Argument1 = activityBeingValidated, Argument2 = validationContext, Handler = new Sequence() { Variables = { parentIsCallScope, }, Activities = { new ForEach<Activity>() { Values = new GetParentChain() { ValidationContext = validationContext, }, Body = new ActivityAction<Activity>() { Argument = parent, Handler = new If(env => parent.Get(env).GetType() == typeof(CallScope)) { Then = new Assign<bool>() { To = parentIsCallScope, Value = true, }, }, }, }, new AssertValidation() { Assertion = parentIsCallScope, Message = "Twilio activities must be nested inside of a CallScope", IsWarning = false, }, }, }, }, }; }
// Create a validation to verify that there are no Persist activites inside of a NoPersistScope static Constraint VerifiyNoChildPersistActivity() { DelegateInArgument<NoPersistScope> element = new DelegateInArgument<NoPersistScope>(); DelegateInArgument<ValidationContext> context = new DelegateInArgument<ValidationContext>(); DelegateInArgument<Activity> child = new DelegateInArgument<Activity>(); Variable<bool> result = new Variable<bool>(); return new Constraint<NoPersistScope> { Body = new ActivityAction<NoPersistScope, ValidationContext> { Argument1 = element, Argument2 = context, Handler = new Sequence { Variables = { result }, Activities = { new ForEach<Activity> { Values = new GetChildSubtree { ValidationContext = context }, Body = new ActivityAction<Activity> { Argument = child, Handler = new If() { Condition = new InArgument<bool>((env) => object.Equals(child.Get(env).GetType(),typeof(Persist))), Then = new Assign<bool> { Value = true, To = result } } } }, new AssertValidation { Assertion = new InArgument<bool>(env => !result.Get(env)), Message = new InArgument<string> ("NoPersistScope activity can't contain a Persist activity"), PropertyName = new InArgument<string>((env) => element.Get(env).DisplayName) } } } } }; }
/// <summary> /// Validates whether the Leave activity is contained inside of an Enqueue activity. /// </summary> /// <returns></returns> Constraint<Leave> MustBeContainedInEnqueue() { var activityBeingValidated = new DelegateInArgument<Leave>(); var validationContext = new DelegateInArgument<ValidationContext>(); var outer = new DelegateInArgument<Activity>(); var enqueueIsOuter = new Variable<bool>(); return new Constraint<Leave>() { Body = new ActivityAction<Leave, ValidationContext>() { Argument1 = activityBeingValidated, Argument2 = validationContext, Handler = new Sequence() { Variables = { enqueueIsOuter, }, Activities = { new ForEach<Activity>() { Values = new GetParentChain() { ValidationContext = validationContext, }, Body = new ActivityAction<Activity>() { Argument = outer, Handler = new If(env => typeof(Enqueue).IsAssignableFrom(outer.Get(env).GetType())) { Then = new Assign<bool>() { To = enqueueIsOuter, Value = true, }, }, }, }, new AssertValidation() { Assertion = enqueueIsOuter, Message = "Leave must be contained inside of an Enqueue.", IsWarning = false, }, }, }, }, }; }
static Constraint CheckParent() { DelegateInArgument<CreateState> element = new DelegateInArgument<CreateState>(); DelegateInArgument<ValidationContext> context = new DelegateInArgument<ValidationContext>(); Variable<bool> result = new Variable<bool>(); DelegateInArgument<Activity> parent = new DelegateInArgument<Activity>(); return new Constraint<CreateState> { Body = new ActivityAction<CreateState,ValidationContext> { Argument1 = element, Argument2 = context, Handler = new Sequence { Variables = { result }, Activities = { new ForEach<Activity> { Values = new GetParentChain { ValidationContext = context }, Body = new ActivityAction<Activity> { Argument = parent, Handler = new If() { Condition = new InArgument<bool>((env) => object.Equals(parent.Get(env).GetType(),typeof(CreateCountry))), Then = new Assign<bool> { Value = true, To = result } } } }, new AssertValidation { Assertion = new InArgument<bool>(result), Message = new InArgument<string> ("CreateState has to be inside a CreateCountry activity"), } } } } }; }
public static Constraint VerifyParentIsObjectContextScope(Activity activity) { DelegateInArgument<Activity> element = new DelegateInArgument<Activity>(); DelegateInArgument<ValidationContext> context = new DelegateInArgument<ValidationContext>(); DelegateInArgument<Activity> child = new DelegateInArgument<Activity>(); Variable<bool> result = new Variable<bool>(); return new Constraint<Activity> { Body = new ActivityAction<Activity, ValidationContext> { Argument1 = element, Argument2 = context, Handler = new Sequence { Variables = { result }, Activities = { new ForEach<Activity> { Values = new GetParentChain { ValidationContext = context }, Body = new ActivityAction<Activity> { Argument = child, Handler = new If { Condition = new InArgument<bool>((env) => object.Equals(child.Get(env).GetType(), typeof(ObjectContextScope))), Then = new Assign<bool> { Value = true, To = result } } } }, new AssertValidation { Assertion = new InArgument<bool>(env => result.Get(env)), Message = new InArgument<string> (string.Format("{0} can only be added inside an ObjectContextScope activity.", activity.GetType().Name)), PropertyName = new InArgument<string>((env) => element.Get(env).DisplayName) } } } } }; }
// Constraint is just an activity that contains validation logic. public static Constraint ConstraintError_IfShouldHaveThenOrElse() { DelegateInArgument<If> element = new DelegateInArgument<If>(); return new Constraint<If> { Body = new ActivityAction<If, ValidationContext> { Argument1 = element, Handler = new AssertValidation { Assertion = new InArgument<bool>(env => (element.Get(env).Then != null) || (element.Get(env).Else != null)), Message = new InArgument<string>("'If' activity should have either Then or Else activity set."), } } }; }
public static Constraint ConstraintWarning_PickHasOneBranch() { DelegateInArgument<Pick> element = new DelegateInArgument<Pick>(); return new Constraint<Pick> { Body = new ActivityAction<Pick, ValidationContext> { Argument1 = element, Handler = new AssertValidation { IsWarning = true, Assertion = new InArgument<bool>(env => (element.Get(env).Branches.Count == 0) || (element.Get(env).Branches.Count > 1)), Message = new InArgument<string>("This Pick activity has only one branch so the Pick itself is redundant."), } } }; }
static RangeEnumeration Enumerate(int start, int stop, int step) { Console.WriteLine("Starting enumeration of a series of numbers from {0} to {1} using steps of {2}", start, stop, step); DelegateInArgument<int> loopVariable = new DelegateInArgument<int>(); return new RangeEnumeration { Start = start, Stop = stop, Step = step, Body = new ActivityAction<int> { Argument = loopVariable, Handler = new WriteLine { Text = new InArgument<string>(context => "This is " + loopVariable.Get(context).ToString()), }, } }; }
static Activity CreateWF() { DelegateInArgument<string> current = new DelegateInArgument<string>(); IList<string> data = new List<string>(); for (int i = 1; i < 11; i++) { data.Add(string.Format("Branch {0}", i)); } Variable<int> waitTime = new Variable<int>(); return new Sequence { Variables = { waitTime }, Activities = { new ThrottledParallelForEach<string> { Values = new LambdaValue<IEnumerable<string>>(c => data), MaxConcurrentBranches = 3, Body = new ActivityAction<string> { Argument = current, Handler = new Sequence { Activities = { new WriteLine() { Text = new InArgument<string>(ctx => string.Format("Enter {0}", current.Get(ctx))) }, new Assign<int> { To = waitTime, Value = new InArgument<int>(ctx => new Random().Next(0, 2500)) }, new WriteLine() { Text = new InArgument<string>(ctx => string.Format("...{0} will wait for {1} millisenconds...", current.Get(ctx), waitTime.Get(ctx))) }, new Delay { Duration = new InArgument<TimeSpan>(ctx => new TimeSpan(0,0,0,0, waitTime.Get(ctx))) }, new WriteLine() { Text = new InArgument<string>(ctx => string.Format("......Exit {0}", current.Get(ctx))) }, } } } } } }; }
public void DialSipTest() { var c = new DelegateInArgument<CallContext>(); var a = new CallScope() { Body = new ActivityAction<CallContext>() { Argument = c, Handler = new Dial() { Activities = { new DialSip() { Uris = { new DialSipUri() { Uri = "*****@*****.**", UserName = "******", }, new DialSipUri() { Uri = "*****@*****.**", UserName = "******", }, }, }, }, }, }, }; var ctx = CreateContext(a); ctx.Invoke(); Assert.AreEqual("<Response><Dial>+15555555555</Dial></Response>", ctx.Response.ToString(SaveOptions.DisableFormatting)); }
static void Main() { ShowDateTime workflow1 = new ShowDateTime(); WorkflowInvoker.Invoke(workflow1); DelegateInArgument<string> str = new DelegateInArgument<string>(); ShowDateTimeAsAction workflow2 = new ShowDateTimeAsAction() { CustomAction = new ActivityAction<string>() { Argument = str, Handler = new WriteLine() { Text = new InArgument<string>(str) } } }; WorkflowInvoker.Invoke(workflow2); Console.WriteLine("Hit <enter> to exit..."); Console.ReadLine(); }
static void Main(string[] args) { // iteration variable for the ForEach var item = new DelegateInArgument<object>(); // list of elements to iterate ArrayList list = new ArrayList(); list.Add("Bill"); list.Add("Steve"); list.Add("Ray"); // iterate through the list and show the elements Activity act = new ParallelForEach { Values = new InArgument<IEnumerable>(ctx => list), Body = new ActivityAction<object> { Argument = item, Handler = new InvokeMethod { TargetType = typeof(Program), MethodName = "ShowThreadId", RunAsynchronously = true, Parameters = { new InArgument<string>(c => item.Get(c).ToString()) } } } }; WorkflowInvoker.Invoke(act); Console.WriteLine(""); Console.WriteLine("Press enter to exit..."); Console.ReadLine(); }
/// <summary> /// Initializes a new instance. /// </summary> /// <param name="activity"></param> public TwilioTestContext(Activity activity) { // wrap in CallScope var arg1 = new DelegateInArgument<CallContext>(); var scope = new CallScope() { Body = new ActivityAction<CallContext>() { Argument = arg1, Handler = activity, }, }; // workflow needs a sync context to run on sync = new SynchronizedSynchronizationContext(); // new invoker which uses ourself as the context app = new WorkflowApplication(scope); app.Extensions.Add<ITwilioContext>(() => this); app.Completed = a => state = a.CompletionState; response = new XElement("Response"); element = response; }
public static Constraint SetReceiveRequestSendResponseScopeExecutionPropertyFactory() { DelegateInArgument<ReceiveRequestSendResponseScope> element = new DelegateInArgument<ReceiveRequestSendResponseScope>(); DelegateInArgument<ValidationContext> context = new DelegateInArgument<ValidationContext>(); DelegateInArgument<Activity> child = new DelegateInArgument<Activity>(); return new Constraint<ReceiveRequestSendResponseScope> { Body = new ActivityAction<ReceiveRequestSendResponseScope, ValidationContext> { Argument1 = element, Argument2 = context, Handler = new ForEach<Activity> { Values = new GetChildSubtree { ValidationContext = context, }, Body = new ActivityAction<Activity> { Argument = child, Handler = new If { Condition = new InArgument<bool>((env) => child.Get(env) is ISendResponse), Then = new ReceiveRequestSendResponseScope.ReceiveRequestSendResponseScopeExecutionPropertyFactorySetter { ISendResponse = new InArgument<ISendResponse>((env) => child.Get(env) as ISendResponse), ReceiveRequestSendResponseScope = new InArgument<ReceiveRequestSendResponseScope>((env) => element.Get(env)), }, }, }, }, }, }; }
private void ProcessStates(NativeActivityMetadata metadata) { foreach (System.Activities.Statements.State state in this.states.Distinct<System.Activities.Statements.State>()) { InternalState internalState = state.InternalState; this.internalStates.Add(internalState); DelegateInArgument<StateMachineEventManager> argument = new DelegateInArgument<StateMachineEventManager>(); internalState.EventManager = argument; ActivityFunc<StateMachineEventManager, string> activityDelegate = new ActivityFunc<StateMachineEventManager, string> { Argument = argument, Handler = internalState }; if (state.Reachable) { metadata.AddDelegate(activityDelegate); } this.internalStateFuncs.Add(activityDelegate); } }
public static Constraint VerifyReceiveRequestSendResponseScopeChildren() { DelegateInArgument<ReceiveRequestSendResponseScope> element = new DelegateInArgument<ReceiveRequestSendResponseScope>(); DelegateInArgument<ValidationContext> context = new DelegateInArgument<ValidationContext>(); DelegateInArgument<Activity> child = new DelegateInArgument<Activity>(); Variable<int> receiveRequestCounter = new Variable<int>(); Variable<int> sendResponseCounter = new Variable<int>(); return new Constraint<ReceiveRequestSendResponseScope> { Body = new ActivityAction<ReceiveRequestSendResponseScope, ValidationContext> { Argument1 = element, Argument2 = context, Handler = new Sequence { Variables = { receiveRequestCounter, sendResponseCounter, }, Activities = { new Assign<int> { Value = 0, To = receiveRequestCounter, }, new Assign<int> { Value = 0, To = sendResponseCounter, }, new ForEach<Activity> { Values = new GetChildSubtree { ValidationContext = context, }, Body = new ActivityAction<Activity> { Argument = child, Handler = new Sequence { Activities = { new If { Condition = new InArgument<bool>((env) => child.Get(env) is IReceiveRequest), Then = new Assign<int> { Value = new InArgument<int>((env) => receiveRequestCounter.Get(env) + 1), To = receiveRequestCounter }, }, new If { Condition = new InArgument<bool>((env) => child.Get(env) is ISendResponse), Then = new Assign<int> { Value = new InArgument<int>((env) => sendResponseCounter.Get(env) + 1), To = sendResponseCounter }, }, }, }, }, }, new AssertValidation { Assertion = new InArgument<bool>((env) => receiveRequestCounter.Get(env) == 1), Message = new InArgument<string> ($"{nameof(ReceiveRequestSendResponseScope)} activity must contain one and only one {nameof(ReceiveRequest)} activity"), PropertyName = new InArgument<string>((env) => element.Get(env).DisplayName) }, new AssertValidation { Assertion = new InArgument<bool>((env) => sendResponseCounter.Get(env) == 1), Message = new InArgument<string> ($"{nameof(ReceiveRequestSendResponseScope)} activity must contain one and only one {nameof(SendResponse)} activity"), PropertyName = new InArgument<string>((env) => element.Get(env).DisplayName) }, }, }, }, }; }
public static Constraint SetWorkflowInterfaceOperationNames() { DelegateInArgument<ReceiveRequestSendResponseScope> element = new DelegateInArgument<ReceiveRequestSendResponseScope>(); DelegateInArgument<ValidationContext> context = new DelegateInArgument<ValidationContext>(); DelegateInArgument<Activity> parent = new DelegateInArgument<Activity>(); DelegateInArgument<Activity> child = new DelegateInArgument<Activity>(); Variable<IOperationActivity> receiveRequest = new Variable<IOperationActivity>(); Variable<Type> workflowInterfaceType = new Variable<Type>(); Variable<Type> requestResultType = new Variable<Type>(); Variable<Type> responseParameterType = new Variable<Type>(); return new Constraint<ReceiveRequestSendResponseScope> { Body = new ActivityAction<ReceiveRequestSendResponseScope, ValidationContext> { Argument1 = element, Argument2 = context, Handler = new Sequence { Variables = { receiveRequest, workflowInterfaceType, requestResultType, responseParameterType, }, Activities = { new ForEach<Activity> { Values = new GetParentChain { ValidationContext = context, }, Body = new ActivityAction<Activity> { Argument = parent, Handler = new If { Condition = new InArgument<bool>((env) => parent.Get(env).IsWorkflowActivity()), Then = new Assign<Type> { Value = new InArgument<Type>((env) => parent.Get(env).GetWorkflowActivityType().GetGenericArguments()[(int)TypeParameterIndex.WorkflowInterface]), To = workflowInterfaceType, }, }, }, }, new ForEach<Activity> { Values = new GetChildSubtree { ValidationContext = context, }, Body = new ActivityAction<Activity> { Argument = child, Handler = new Sequence { Activities = { new If { Condition = new InArgument<bool>((env) => child.Get(env) is IReceiveRequest), Then = new Sequence { Activities = { new Assign<IOperationActivity> { Value = new InArgument<IOperationActivity>((env) => child.Get(env) as IOperationActivity), To = receiveRequest }, new Assign<Type> { Value = new InArgument<Type>((env) => (child.Get(env) as IReceiveRequest).RequestResultType), To = requestResultType }, }, }, }, new If { Condition = new InArgument<bool>((env) => child.Get(env) is ISendResponse), Then = new Assign<Type> { Value = new InArgument<Type>((env) => (child.Get(env) as ISendResponse).ResponseParameterType), To = responseParameterType }, }, }, }, }, }, new WorkflowInterfaceOperationNamesSetter { ReceiveRequest = new InArgument<IOperationActivity>((env) => receiveRequest.Get(env)), WorkflowInterfaceType = new InArgument<Type>((env) => workflowInterfaceType.Get(env)), RequestResultType = new InArgument<Type>((env) => requestResultType.Get(env)), ResponseParameterType = new InArgument<Type>((env) => responseParameterType.Get(env)), }, }, }, }, }; }
private static Activity CreateFlowchartWithFaults(string promoCode, int numKids) { Variable<string> promo = new Variable<string> { Default = promoCode }; Variable<int> numberOfKids = new Variable<int> { Default = numKids }; Variable<double> discount = new Variable<double>(); DelegateInArgument<DivideByZeroException> ex = new DelegateInArgument<DivideByZeroException>(); FlowStep discountNotApplied = new FlowStep { Action = new WriteLine { DisplayName = "WriteLine: Discount not applied", Text = "Discount not applied" }, Next = null }; FlowStep discountApplied = new FlowStep { Action = new WriteLine { DisplayName = "WriteLine: Discount applied", Text = "Discount applied " }, Next = null }; FlowDecision flowDecision = new FlowDecision { Condition = ExpressionServices.Convert<bool>((ctx) => discount.Get(ctx) > 0), True = discountApplied, False = discountNotApplied }; FlowStep singleStep = new FlowStep { Action = new Assign { DisplayName = "discount = 10.0", To = new OutArgument<double> (discount), Value = new InArgument<double> (10.0) }, Next = flowDecision }; FlowStep mnkStep = new FlowStep { Action = new Assign { DisplayName = "discount = 15.0", To = new OutArgument<double> (discount), Value = new InArgument<double> (15.0) }, Next = flowDecision }; FlowStep mwkStep = new FlowStep { Action = new TryCatch { DisplayName = "Try/Catch for Divide By Zero Exception", Try = new Assign { DisplayName = "discount = 15 + (1 - 1/numberOfKids)*10", To = new OutArgument<double>(discount), Value = new InArgument<double>((ctx) => (15 + (1 - 1 / numberOfKids.Get(ctx)) * 10)) }, Catches = { new Catch<System.DivideByZeroException> { Action = new ActivityAction<System.DivideByZeroException> { Argument = ex, DisplayName = "ActivityAction - DivideByZeroException", Handler = new Sequence { DisplayName = "Divide by Zero Exception Workflow", Activities = { new WriteLine() { DisplayName = "WriteLine: DivideByZeroException", Text = "DivideByZeroException: Promo code is MWK - but number of kids = 0" }, new Assign<double> { DisplayName = "Exception - discount = 0", To = discount, Value = new InArgument<double>(0) } } } } } } }, Next = flowDecision }; FlowStep discountDefault = new FlowStep { Action = new Assign<double> { DisplayName = "Default discount assignment: discount = 0", To = discount, Value = new InArgument<double>(0) }, Next = flowDecision }; FlowSwitch<string> promoCodeSwitch = new FlowSwitch<string> { Expression = promo, Cases = { { "Single", singleStep }, { "MNK", mnkStep }, { "MWK", mwkStep } }, Default = discountDefault }; Flowchart flowChart = new Flowchart { DisplayName = "Promotional Discount Calculation", Variables = {discount, promo, numberOfKids}, StartNode = promoCodeSwitch, Nodes = { promoCodeSwitch, singleStep, mnkStep, mwkStep, discountDefault, flowDecision, discountApplied, discountNotApplied } }; return flowChart; }
/// <summary> /// Create the structure of the activity for pausing between successive polling activities. /// </summary> /// <returns>An activity delegate.</returns> private static ActivityAction<int> CreateDelayBody() { var timeout = new DelegateInArgument<int> { Name = "FuncTimeout" }; return new ActivityAction<int> { Argument = timeout, Handler = new Delay { Duration = new InArgument<TimeSpan>(ctx => TimeSpan.FromSeconds(timeout.Get(ctx))) } }; }
private Constraint ValidateActivitiesConstraint(string runtimeAssembly, PSWorkflowValidationResults validationResults) { DelegateInArgument<Activity> activityToValidate = new DelegateInArgument<Activity>(); DelegateInArgument<ValidationContext> validationContext = new DelegateInArgument<ValidationContext>(); return new Constraint<Activity> { Body = new ActivityAction<Activity, ValidationContext> { Argument1 = activityToValidate, Argument2 = validationContext, Handler = new AssertValidation { IsWarning = false, Assertion = new InArgument<bool>( env => ValidateActivity(activityToValidate.Get(env), runtimeAssembly, validationResults)), Message = new InArgument<string>( env => string.Format(CultureInfo.CurrentCulture, Resources.InvalidActivity, activityToValidate.Get(env).GetType().FullName)) } } }; }