Thursday, November 7, 2013

System.Xml.XmlDocument is not marked as serializable in BizTalk

XmlDocument is not a serializable type.

     This error usually occurs when an orchestration (which uses any of your assembly that has XmlDocument type field) is going to be dehydrated, the BizTalk engine is going to serialize every variable.

     This won't apply to variables that are of the XmlDocument type in the orchestration as BizTalk knows it's not serializable and won't try. Because this is so widely used that it is treated as a special case. The BizTalk Engine knows how to serialize it so the XLang compiler doesn't raise an error. The serialization process saves the type of the variable and the value of the OuterXml property, which can then later be used to instantiate a matching
System.Xml.XmlDocument when deserializing.


I got this exception when we want to receive a response type as System.Xml.XmlDocument from a C# class. or if we have System.Xml.XmlDocument object declaration at class level.

 Use Default Constructor property causes an instance of the object to be constructed when the orchestration instantiates. If this property is set to False, the variable will need to be instantiated in an Expression shape through the new keyword. Try this, otherwise use below option.
                                      OR

Mark the member as NonSerializable. BizTalk will not attempt to serialize the object and you shouldn't see this error
                                     OR
The solution is very simple: if you're using, in any of your orchestrations, assemblies that have a XmlDocument type field as a response. Then make that class and members are static (No instance, no persistence, no serialization).


Possible error is:
Type 'System.Xml.XmlDocument' in Assembly 'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.

   




6 comments:

  1. Thank you for this, spent 2 hours trying to figure this out

    ReplyDelete
  2. I cannot set Default Constructor to false in my orchestration. Can you provide more details on how to set that class and its' members as static?

    Thank you

    ReplyDelete
    Replies
    1. You can set this property for your class object (declared in the orchestration variables group) properties window in orchestration.

      Delete
    2. I am using BizTalk 2010, I can't find that option in properties window

      Delete
    3. Please find the attached screenshot in this post.

      Delete