private void miCreateSequentialWorkflow_Click(object sender, EventArgs e) { // create a new sequential workflow WorkflowInstance workflowInstance = SampleWorkflowRuntime.Current.CreateSequentialWorkflow(); if (_options.WorkflowSettings.ModifySequentialWorkflow) { Activity rootActivity = workflowInstance.GetWorkflowDefinition(); WorkflowChanges workflowChanges = new WorkflowChanges(rootActivity); // modify the workflow Activity activityToRemove = workflowChanges.TransientWorkflow.GetActivityByName("codeActivity3"); CompositeActivity parentActivity = activityToRemove.Parent; parentActivity.Activities.Remove(activityToRemove); CodeActivity codeActivity = new CodeActivity("TestChangeActivity"); codeActivity.ExecuteCode += delegate { Trace.WriteLine("Test Change Activity executed..."); }; parentActivity.Activities.Add(codeActivity); workflowInstance.ApplyWorkflowChanges(workflowChanges); } workflowInstance.Start(); SampleWorkflowRuntime.Current.RunWorkflow(workflowInstance.InstanceId); }
public void ApplyWorkflowChanges(WorkflowChanges workflowChanges) { using (new WorkflowTraceTransfer(this.InstanceId)) { WorkflowExecutor executor; Label_000C: executor = this._runtime.Load(this); if (!executor.IsInstanceValid) { goto Label_000C; } try { executor.ApplyWorkflowChanges(workflowChanges); } catch (InvalidOperationException) { if (executor.IsInstanceValid) { throw; } goto Label_000C; } } }
public void ApplyWorkflowChanges(WorkflowChanges workflowChanges) { using (new WorkflowTraceTransfer(this.InstanceId)) { while (true) { WorkflowExecutor executor = _runtime.Load(this); if (executor.IsInstanceValid) { try { executor.ApplyWorkflowChanges(workflowChanges); break; } catch (InvalidOperationException) { if (executor.IsInstanceValid) { throw; } } } } } }
private void btnCreateSequentialWorkflow_Click(Object sender, EventArgs e) { ConnectionStringSettings persistenceConnectionString = cboPersistenceService.SelectedItem as ConnectionStringSettings; ConnectionStringSettings trackingConnectionString = cboTrackingService.SelectedItem as ConnectionStringSettings; if (persistenceConnectionString == null) { MessageBox.Show("No connection string selected for persistence service.", "WFTools Samples", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } String workflowRuntimeKey = String.Format("{0}_{1}_{2}", persistenceConnectionString.Name, trackingConnectionString == null ? "None" : trackingConnectionString.Name, chkUseLocalTransactions.Checked); SampleWorkFlowRuntime workflowRuntime; if (!this.loadedWorkflowRuntimes.TryGetValue(workflowRuntimeKey, out workflowRuntime)) { workflowRuntime = new SampleWorkFlowRuntime(persistenceConnectionString, trackingConnectionString, chkUseLocalTransactions.Checked); workflowRuntime.WorkflowTerminated += workflowRuntime_WorkflowTerminated; workflowRuntime.ServicesExceptionNotHandled += workflowRuntime_ServicesExceptionNotHandled; this.loadedWorkflowRuntimes.Add(workflowRuntimeKey, workflowRuntime); } // create a new sequential workflow WorkflowInstance workflowInstance = workflowRuntime.CreateSequentialWorkflow(); if (chkModifyWorkflow.Checked) { Activity rootActivity = workflowInstance.GetWorkflowDefinition(); WorkflowChanges workflowChanges = new WorkflowChanges(rootActivity); // modify the workflow Activity activityToRemove = workflowChanges.TransientWorkflow.GetActivityByName("codeActivity3"); CompositeActivity parentActivity = activityToRemove.Parent; parentActivity.Activities.Remove(activityToRemove); CodeActivity codeActivity = new CodeActivity("TestChangeActivity"); codeActivity.ExecuteCode += delegate { Trace.WriteLine("Test Change Activity executed..."); }; parentActivity.Activities.Add(codeActivity); workflowInstance.ApplyWorkflowChanges(workflowChanges); } workflowInstance.Start(); ManualWorkflowSchedulerService schedulerService = workflowRuntime.GetService <ManualWorkflowSchedulerService>(); schedulerService.RunWorkflow(workflowInstance.InstanceId); }
/// <summary> /// Apply the changes /// </summary> /// <param name="instance"></param> /// <param name="wfChanges"></param> private static void ValidateAndApplyChanges( WorkflowInstance instance, WorkflowChanges wfChanges) { //validate the structural changes before applying them ValidationErrorCollection errors = wfChanges.Validate(); if (errors.Count == 0) { try { //apply the changes to the workflow instance instance.ApplyWorkflowChanges(wfChanges); } catch (Exception e) { Console.WriteLine("Exception applying changes: {0}", e.Message); } } else { //the proposed changes are not valid foreach (ValidationError error in errors) { Console.WriteLine(error.ToString()); } } }
/// <summary> /// Replace the entire rule definition for a workflow /// </summary> /// <param name="instance"></param> private static void ReplaceRuleDefinition(WorkflowInstance instance) { //create a workflow changes object WorkflowChanges wfChanges = new WorkflowChanges( instance.GetWorkflowDefinition()); //get a stream from an externally saved .rules file Stream stream = new FileStream(@"ModifiedRule.rules", FileMode.Open, FileAccess.Read, FileShare.Read); //read the .rules file using an XmlReader using (XmlReader xmlReader = XmlReader.Create( new StreamReader(stream))) { WorkflowMarkupSerializer markupSerializer = new WorkflowMarkupSerializer(); //deserialize the rule definitions RuleDefinitions ruleDefinitions = markupSerializer.Deserialize(xmlReader) as RuleDefinitions; if (ruleDefinitions != null) { //replace the embedded rules definition //with the new one that was deserialzed from a file wfChanges.TransientWorkflow.SetValue( RuleDefinitions.RuleDefinitionsProperty, ruleDefinitions); ValidateAndApplyChanges(instance, wfChanges); } } }
/// <summary> /// Modify a single rule condition /// </summary> /// <param name="instance"></param> private static void ModifyRuleCondition(WorkflowInstance instance) { //create a workflow changes object WorkflowChanges wfChanges = new WorkflowChanges( instance.GetWorkflowDefinition()); //retrieve the RuleDefinitions for the workflow RuleDefinitions ruleDefinitions = (RuleDefinitions)wfChanges.TransientWorkflow.GetValue( RuleDefinitions.RuleDefinitionsProperty); if (ruleDefinitions != null) { if (ruleDefinitions.Conditions.Contains("conditionOne")) { //retrieve the rule that we want to change RuleExpressionCondition condition = ruleDefinitions.Conditions["conditionOne"] as RuleExpressionCondition; //change the rule by setting the right side of the //operation to 300 instead of the original value of 100. //was: this.TestNumber > 100 //now: this.TestNumber > 300 CodeBinaryOperatorExpression codeExpression = condition.Expression as CodeBinaryOperatorExpression; codeExpression.Right = new CodePrimitiveExpression(300); ValidateAndApplyChanges(instance, wfChanges); } } }
private void OnAddApprovalStep(object sender, EventArgs e) { InvokeWorkflowActivity invokeApprovalStepWorkflow = new InvokeWorkflowActivity(); // // use WorkflowChanges class to author dynamic change // WorkflowChanges changes = new WorkflowChanges(this); // // setup to invoke ApprovalStepWorkflow type Type type = typeof(ApprovalStepWorkflow); invokeApprovalStepWorkflow.Name = "AddApprovalStepWorkflow"; invokeApprovalStepWorkflow.TargetWorkflow = type; // // insert invokeApprovalStepWorkflow in ifElseApproval transient activity collection // CompositeActivity checkApproval = changes.TransientWorkflow.Activities["CheckApproval"] as CompositeActivity; CompositeActivity approvedBranch = checkApproval.Activities["Approved"] as CompositeActivity; approvedBranch.Activities.Add(invokeApprovalStepWorkflow); // // apply transient changes to instance // this.ApplyWorkflowChanges(changes); Console.WriteLine(" Added an InvokeWorkflow activity within the workflow to approve the PO"); }
static void OnWorkflowIdle(object sender, WorkflowEventArgs e) { if (wasChanged) { return; } wasChanged = true; WorkflowInstance workflowInstance = e.WorkflowInstance; Int32 newAmount = 15000; Console.WriteLine("Dynamically change approved amount to {0:c}", newAmount); // Dynamic update of order rule WorkflowChanges workflowchanges = new WorkflowChanges(workflowInstance.GetWorkflowDefinition()); CompositeActivity transient = workflowchanges.TransientWorkflow; RuleDefinitions ruleDefinitions = (RuleDefinitions)transient.GetValue(RuleDefinitions.RuleDefinitionsProperty); RuleConditionCollection conditions = ruleDefinitions.Conditions; RuleExpressionCondition condition1 = (RuleExpressionCondition)conditions["Check"]; (condition1.Expression as CodeBinaryOperatorExpression).Right = new CodePrimitiveExpression(newAmount); workflowInstance.ApplyWorkflowChanges(workflowchanges); }
/// <summary> /// Add a new custom activity to the workflow instance /// </summary> /// <param name="instance"></param> private static void AddNewActivity(WorkflowInstance instance) { //create a workflow changes object WorkflowChanges wfChanges = new WorkflowChanges( instance.GetWorkflowDefinition()); //find the SequenceActivity that is a placeholder //for new activities CompositeActivity placeholder = wfChanges.TransientWorkflow.GetActivityByName( "sequencePlaceholder") as CompositeActivity; if (placeholder != null) { //create an instance of the new activity NewFunctionActivity newActivity = new NewFunctionActivity(); //bind the TestNumber property of the activity //to the TestNumber property of the workflow #if UPDATES_RESTRICTED newActivity.SetBinding( NewFunctionActivity.TestNumberProperty, new ActivityBind("DynamicRestrictedWorkflow", "TestNumber")); #else newActivity.SetBinding( NewFunctionActivity.TestNumberProperty, new ActivityBind("DynamicWorkflow", "TestNumber")); #endif //add the new custom activity to the workflow placeholder.Activities.Add(newActivity); //apply the changes ValidateAndApplyChanges(instance, wfChanges); } }
private void AddOrderOnHoldState() { // Get a reference to the WorkflowInstance for the selected workflow WorkflowInstance instance = this.runtime.GetWorkflow(this.GetSelectedWorkflowInstanceID()); // Get a reference to the root activity for the workflow Activity root = instance.GetWorkflowDefinition(); // Create a new instance of the WorkflowChanges class for managing // the in-memory changes to the workflow WorkflowChanges changes = new WorkflowChanges(root); // Create a new State activity to the workflow StateActivity orderOnHoldState = new StateActivity(); orderOnHoldState.Name = "OrderOnHoldState"; // Add a new EventDriven activity to the State EventDrivenActivity eventDrivenDelay = new EventDrivenActivity(); eventDrivenDelay.Name = "DelayOrderEvent"; orderOnHoldState.Activities.Add(eventDrivenDelay); // Add a new Delay, initialized to 5 seconds DelayActivity delayOrder = new DelayActivity(); delayOrder.Name = "delayOrder"; delayOrder.TimeoutDuration = new TimeSpan(0, 0, 5); eventDrivenDelay.Activities.Add(delayOrder); // Add a new SetState to the OrderOpenState SetStateActivity setStateOrderOpen = new SetStateActivity(); setStateOrderOpen.TargetStateName = "OrderOpenState"; eventDrivenDelay.Activities.Add(setStateOrderOpen); // Add the OnHoldState to the workflow changes.TransientWorkflow.Activities.Add(orderOnHoldState); // Apply the changes to the workflow instance try { instance.ApplyWorkflowChanges(changes); } catch (WorkflowValidationFailedException) { // New state has already been added MessageBox.Show("On Hold state has already been added to this workflow."); } }
/// <summary> /// Add a new custom activity to this workflow instance /// </summary> /// <param name="instance"></param> private void AddNewActivity() { //create an instance of the specified new activity if (NewActivityType != null && NumberProperty != null) { //create a workflow changes object WorkflowChanges wfChanges = new WorkflowChanges(this); //find the SequenceActivity that is a placeholder //for new activities CompositeActivity placeholder = wfChanges.TransientWorkflow.GetActivityByName( "sequencePlaceholder") as CompositeActivity; if (placeholder == null) { return; } //construct an instance of the activity //using reflection ConstructorInfo ctor = NewActivityType.GetConstructor(Type.EmptyTypes); Activity newActivity = ctor.Invoke(null) as Activity; //bind the TestNumber property of the activity //to the TestNumber property of the workflow newActivity.SetBinding(NumberProperty, new ActivityBind(this.Name, "TestNumber")); //add the new custom activity to the workflow placeholder.Activities.Add(newActivity); //validate the structural changes before applying them ValidationErrorCollection errors = wfChanges.Validate(); if (errors.Count == 0) { //apply the changes to the workflow instance this.ApplyWorkflowChanges(wfChanges); } else { //the proposed changes are not valid foreach (ValidationError error in errors) { Console.WriteLine(error.ToString()); } } } }
/// <summary> /// Delete an activity from the workflow instance /// </summary> /// <param name="instance"></param> private static void DeleteActivity(WorkflowInstance instance) { //create a workflow changes object WorkflowChanges wfChanges = new WorkflowChanges( instance.GetWorkflowDefinition()); //find the activity we want to remove Activity activity = wfChanges.TransientWorkflow.GetActivityByName( "codeFirstPart"); if (activity != null) { //remove the activity from its parent activity.Parent.Activities.Remove(activity); //apply the changes ValidateAndApplyChanges(instance, wfChanges); } }
static void OnWorkflowIdled(object sender, WorkflowEventArgs e) { WorkflowInstance workflowInstance = e.WorkflowInstance; Activity wRoot = workflowInstance.GetWorkflowDefinition(); // // use WorkflowChanges class to author dynamic change // WorkflowChanges changes = new WorkflowChanges(wRoot); Console.WriteLine(" Host is denying all PO requests - Removing POCreated step"); // // remove POCreated activity // changes.TransientWorkflow.Activities.Remove(changes.TransientWorkflow.Activities["POCreated"]); // // apply transient changes to instance // workflowInstance.ApplyWorkflowChanges(changes); }