Dynamic Update from Custom Activity
-
jeudi 28 septembre 2006 14:04
How can I do Dynamic Update from Custom Activity? I could not access ApplyWorkflowChanges() from within the Custom Activity.
Any ideas to solve this are appeciated.
Thanks
Toutes les réponses
-
jeudi 28 septembre 2006 17:27
You can create a WorkflowRuntimeService that does the work:
in a hurry, I would write this :
step 1 class MyUpdateService : WorkflowRuntimeService {
public void MakeTheUpdateHere(Guid aGuid)
{
WorkflowInstance instance = this.Runtime.GetWorkflow(aGuid)
...
instance.ApplyWorkflowChanges(...)
}
}
step 2 Add the service in the workflowRuntime
step 3 In your custom activity :
class MyCustomClass {
protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{MyUpdateService myService = executionContext.GetService<MyUpdateService>();
myService.MakeTheUpdateHere(this.WorkflowInstanceId);
return ActivityExecutionStatus.Closed;
}}
-
vendredi 29 septembre 2006 02:50
Use the parent property recursively to move up to the workflow itself (parent is null) or composite activity and then use the ApplyWorkflowChanges method from the composite activity class.
Matt
-
samedi 30 septembre 2006 05:05
Thanks Matt,
BUT the composite activity class does not have ApplyWorkflowChanges method. I have tried this:
Activity parentActivity = executionContext.Activity.Parent; while(parentActivity.Parent != null){
parentActivity = parentActivity.Parent;
};
((
CompositeActivity)parentActivity).<<ApplyWorkflowChanges DOES NOT Exist>>Could you please let me know what is the mistake I am doing? and how to access ApplyWorkflowChanges method from a Custom Activity?
Thanks
-
samedi 30 septembre 2006 05:13
Thanks Serge,
I have tried your suggested solution in the past BUT when I ApplyWorkflowChanges() (when the Workflow Instance is not suspended/idle) I get this error "Instance operation is not valid on workflow runtime thread"! In the past I have also posted this problem in the forum but I got no answer (http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=684011&SiteID=1).
Any further ideas are appreciated.
Thanks
-
samedi 30 septembre 2006 08:22
Hi Karim,
1°If you choose the "Service Way of working" (personally I prefer decoupling my workflow from this kind of stuff)
you can Suspend and Resume it in the service:
public void MakeTheUpdateHere(Guid aGuid)
{
WorkflowInstance instance = this.Runtime.GetWorkflow(aGuid)
insance...Suspend(...);
instance.ApplyWorkflowChanges(...)
instance.Resume();
}
2°If you choose Matt's idea:
Since ApplyWorkfllowChanges is protected, you can provide a public function at the WorkflowActivity level (ex: ApplyChanges) that will call the protected ApplyWorklowChanges
class MyWorkflow : SequentialWorkflowActivity {
public void ApplyChanges(....)
{
this.ApplyWorkflowChanges(...);
}
}
in your activity :
class MyCustomActivity : Activity {
protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{
Activity rootActivity ...get the root activity as you did
((MyWorkflow) rootWorkflow).ApplyChanges(...);
return ActivityExecutionStatus.Closed;
}}
Hope this helps; let me know if that works (I haven't tested it)
Serge
-
jeudi 5 octobre 2006 03:09
Sorry, too quick on the keys, that method is protected.
If your custom activity derives from composite, you should be able to apply changes to your activity and children.
If you need to apply changes to the workflow itself, it might be best to create a custom worklow that derives from the base you wish to use (sequential or state machine) and then open up the ApplyworkflowChanges method.
Matt
-
lundi 28 novembre 2011 00:06
Hi. I coincidentally used a snippet of code similar to yours, trying to suspend a running workflow from a custm workflowRuntimeService I created just for this purpose. Do you know why I get the exception:
System.InvalidOperationException: Instance operation is not valid on workflow runtime thread
at System.Workflow.Runtime.InstanceLock.InstanceLockGuard.EnforceGuard(InstanceLock theLock)
at System.Workflow.Runtime.WorkflowExecutor.Terminate(String error)
at System.Workflow.Runtime.WorkflowInstance.Terminate(String error)My code looks like this:
public class WorkflowControllerService : WorkflowRuntimeService { public void Terminate(Guid workflowId, Guid workflowInstanceId, string siteHost) { // terminate WorkflowRuntime workflowRuntime = this.Runtime; WorkflowInstance instance = workflowRuntime.GetWorkflow(workflowInstanceId); instance.Terminate(string.Empty); } public void Pause(Guid workflowId, Guid workflowInstanceId, string siteHost) { WorkflowRuntime workflowRuntime = this.Runtime; WorkflowInstance instance = workflowRuntime.GetWorkflow(workflowInstanceId); instance.Suspend("Workflow paused because the site was inactive."); } }
I am trying to terminate and pause the executing workflow instance. I understand that i cannot do this directly inside the executing activities of the workflow, so I created a custom service whose methods I call from inside executing activities of that workflow depending on different my application business conditions.Is this the right way to do this?

