Be aware of the serializer when using AppFabric or Workflow Manager

When you develop a custom activity for Workflow Foundation 4.x xamlx workflows think about the persistence of your arguments and context variables.
This are the “Persistence Best Practices” which MSDN recommends:
“A workflow can only be persisted if all of the data types used by the workflow are serializable. In addition, custom types used in persisted workflows must be serializable with NetDataContractSerializer in order to be persisted by SqlWorkflowInstanceStore.” (http://msdn.microsoft.com/en-us/library/ff729670%28v=vs.110%29.aspx)

But what is “serializable” in this context, what serializer is used for serialization?

Every time your workflow hit a persistence point (explicit or implicit [like a delay activity]) your context data will be serialized into your configured persistence store.
At one of our projects a workflow was hosted in Workflow Manager and we end up with a serialization exception for an Exception type (see damirs blog for details: http://developers.de/blogs/damir_dobric/archive/2012/11/16/serialization-of-exceptions-in-workflow-manager.aspx). On another project a workflow was hosted in AppFabric (IIS) and here we end up with a serialization exception at an XmlDocument typed argument of a custom activity. But we although use Exception as argument for a custom logging activity with no issues on serialization.

So I ask myself the question what serializer is used (if you have a link to an official documentation, please leave a comment). It cannot be the same serializer in AppFabric and Workflow Manager.

After some search on the web it seems to be that Workflow Manager uses (good old BizTalk proven) XmlSerializer to serialize persistence data (XmlDocument has a special handling here and Exception is not serializable with XmlSerializer). AppFabric (with SqlWorkflowInstanceStore for persistence) uses the NetDataContractSerializer (XmlDocument cannot be serialized but Exception works well).

So when you write custom activities make sure to check appropriate serialization for your arguments and context variables. Since you can host your xamlx workflow in Workflow Manager and AppFabric check that the types you are using can be serialized with both XmlSerializer and NetDataContractSerializer.

Here is some quick code to check serialization of your types (using one of the thousand console application on your dev machine ;)

static void S1(object x)
{
    var fs = new FileStream(@"c:\temp\text1.xml", FileMode.Create);
    var writer = XmlDictionaryWriter.CreateTextWriter(fs);
 
    var s = new NetDataContractSerializer();
    s.WriteObject(writer, x);
    writer.Close();
}
 
static void S2(object x)
{
    var fs = new FileStream(@"c:\temp\text2.xml", FileMode.Create);
    var writer = XmlDictionaryWriter.CreateTextWriter(fs);
 
    var s = new XmlSerializer(x.GetType());
    s.Serialize(writer, x);
    writer.Close();
}

Posted Nov 26 2013, 02:47 PM by alehmann

Comments

Tory Burch Louiisa wrote re: Be aware of the serializer when using AppFabric or Workflow Manager
on 10-11-2014 17:41

Great tips! I will try to use them. Thanks for any future updates.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
developers.de is a .Net Community Blog powered by daenet GmbH.