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); }
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); }
/// <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()); } } }
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); }
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."); } }
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); }
/// <summary> /// Applies the workflow changes. /// </summary> public void ApplyWorkflowChanges() { if (this.InnerWorkflowChanges != null) { // Load WorkflowInstance WorkflowInstance instance = GlobalWorkflowRuntime.WorkflowRuntime.GetWorkflow(this.InstanceId); // ApplyWorkflowChanges to WorkflowInstance instance.ApplyWorkflowChanges(this.InnerWorkflowChanges); // ApplyWorkflowChanges to WorkflowInstanceEntity this.WorkflowEntity[WorkflowInstanceEntity.FieldXaml] = McWorkflowSerializer.GetString(this.InnerWorkflowChanges.TransientWorkflow); BusinessManager.Update(this.WorkflowEntity); } else if (this.WorkflowEntity != null) { // ApplyWorkflowChanges to WorkflowInstanceEntity this.WorkflowEntity[WorkflowInstanceEntity.FieldXaml] = McWorkflowSerializer.GetString(this.TransientWorkflow); if (this.WorkflowEntity.PrimaryKeyId.HasValue) { BusinessManager.Update(this.WorkflowEntity); } else { this.InstanceId = (Guid)BusinessManager.Create(this.WorkflowEntity); } } else { throw new InvalidOperationException("The workflow has already been saved."); } Dispose(true); }