Windows > Software Development for Windows Client Forums > Windows Workflow Foundation > How to load dynamically changed activites? Error "'Can not add activity in a CompositeActivity which has built in children"
Ask a questionAsk a question
 

AnswerHow to load dynamically changed activites? Error "'Can not add activity in a CompositeActivity which has built in children"

  • Friday, March 30, 2007 3:29 PMMichael Nemtsev [MVP]MVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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"?><ns0Stick out tongueageActivity 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" />

    </ns0Stick out tongueageActivity>

     

     

    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

All Replies