locked
How to serialize an Activity into a database RRS feed

  • Question

  • I have what I believe is a common situation: 

    The system has a whole list of System.Activities.Activity which need to be saved into a database as a blob, one per row.  The system determines which one it needs, loads the blob, converts it back into a System.Activities.Activity and runs it.

    The problem I am having is how to get the activities into the database to begin with.  Since over time there will be new activities, the system is simply using reflection to find all the classes.  The question is how does one serialize the class once it is found.  Here is the code so far:

    var assembly = Assembly.GetAssembly(this.GetType());
    
    foreach (Type objType in assembly.GetTypes())
    {
        if (objType.IsSubclassOf(typeof(Activity)))
        {
            var activity = Activator.CreateInstance(objType) as Activity;
            AddActivity2Database(activity, <other parameters>);
        }
    }
    

    I tried this, but it isn't complete, it is only providing the first element of the XAML file.  I need the whole thing:

    var sb = new StringBuilder();
    using (var writer = XmlTextWriter.Create(sb))
    {
        var serializer = new WorkflowMarkupSerializer();
        serializer.Serialize(writer, activity);
        writer.Close();
    }			

    Any suggestions?
    Wednesday, June 20, 2012 6:36 PM

Answers

  • Hi,

    Base on my understanding, if the workflow definition has been compiled into types in assembly, then the variables and activities of the workflow is invisable even we instantiate the type. An approach here is inspect the activity tree of the compiled type by using WorkflowInspectionServices, then we'll be able to create a ActivityBuilder for serialization. For the detailed information, please refer to:

    Activity Tree Inspection

    http://msdn.microsoft.com/en-us/library/ee358719

    Serializing Workflows and Activities to and from XAML

    http://msdn.microsoft.com/en-us/library/ff458319

    Hope this information helps, thanks.


    Leo Tang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by EHCarleton Friday, June 22, 2012 2:15 PM
    Friday, June 22, 2012 3:01 AM