How to load dynamically changed activites? Error "'Can not add activity in a CompositeActivity which has built in children"
I have the simple SequentialWorkflowActivity, which is dynamically changed - just add new CodeActivity there
using WorkflowMarkupSerializer and then serialized to XAML.
But when I'm trying to deserialize and start activity I got the exception. "Could not deserialize object. Serializer for type 'ConsoleApplication1.PageActivity' threw an exception during deserialization. Exception was thrown with error message 'Can not add activity in a CompositeActivity which has built in children."
What could be the problem and how to fix it?
Code with comments is below
//My Activity
public class PageActivity : SequentialWorkflowActivity{
public PageActivity(){
CodeActivity code = new CodeActivity("TestActivity");code.ExecuteCode +=
delegate { Console.WriteLine("blabla"); };Activities.Add(code);
}
}
//Create new activity
CodeActivity code = new CodeActivity("InnterActivity");
code.ExecuteCode += delegate { Console.WriteLine("Dynamic-bla"); };//Add new activity to the existed one
WorkflowRuntime
workflowRuntime = new WorkflowRuntime();WorkflowInstance
instance;instance = workflowRuntime.CreateWorkflow(
typeof(PageActivity));WorkflowChanges wfChanges = new WorkflowChanges(instance.GetWorkflowDefinition());
wfChanges.TransientWorkflow.Activities.Add(code);instance.ApplyWorkflowChanges(wfChanges);
//Serialize the updated activity
WorkflowMarkupSerializer serializer = new WorkflowMarkupSerializer();
StringBuilder sb = new StringBuilder(); XmlWriter xmlWriter = XmlWriter.Create("workflow.xoml"); PageActivity pa = new PageActivity(); DesignerSerializationManager serializationManager = new DesignerSerializationManager(); using (serializationManager.CreateSession()){
serializer.Serialize(serializationManager, xmlWriter, wfChanges.TransientWorkflow);
}
xmlWriter.Close();
now I have the xaml as
<?xml version="1.0" encoding="utf-8"?><ns0
ageActivity x:Name="PageActivity" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/workflow" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ns0="clr-namespace:ConsoleApplication1;Assembly=ConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<CodeActivity x:Name="TestActivity" />
<CodeActivity x:Name="InnterActivity" />
</ns0
ageActivity>Deseriazing is like
WorkflowInstance
deserializedWorkflow = null; try{
using (XmlReader reader = XmlReader.Create("workflow.xoml")){
deserializedWorkflow = workflowRuntime.CreateWorkflow(reader);
}
}
catch (WorkflowValidationFailedException exp){
ValidationErrorCollection list = exp.Errors; foreach (ValidationError err in list){
//got exception here
Console.WriteLine(err.ErrorText);}
return;}
// Start workflow Console.WriteLine("Starting workflow.");deserializedWorkflow.Start();
Answers
- This is by design - we do not support this scenario in WF V1. The "CanModifyActivity" property of a custom composite is set to false once it has a child activity. So when you add the first code activity, you cannot add any more activities to your Page Activity. if you wanted to add two code activities, dont add the first code at construction time, drop a sequence with code1 and code2 later.
- and this is good too - http://blogs.msdn.com/sergeychub/archive/2006/07/19/672179.aspx
All Replies
- This is by design - we do not support this scenario in WF V1. The "CanModifyActivity" property of a custom composite is set to false once it has a child activity. So when you add the first code activity, you cannot add any more activities to your Page Activity. if you wanted to add two code activities, dont add the first code at construction time, drop a sequence with code1 and code2 later.
- maybe this should be add to KB or smth?
That's a good suggestion, I will pass it along to the PM responsible for this area. We are investigating supporting this in our next version, but it might be useful to have something documented meanwhile.
Thanks
Kavita
- Just wanted to let you know that we do have it in the doc here: Read the portion just above "Custom Toolbox Items"
- and this is good too - http://blogs.msdn.com/sergeychub/archive/2006/07/19/672179.aspx


