You are done with coding and getting ready to test your services. Now you realized that some of your objects/properties are not serializable. Don’t be discouraged, keep reading!
I was working with XElement (a .Net type) as it would allow you to pass any xml. Well, everything was good until I ran the test on SoapUI. I was rewared with sometime like XElement is not marked as serializable when I was testing a .net Remoting service with Soap Formatter! I had to implement my own serialization to overcome the problem. Eventhough this example is showing how to handle serialization of XElement but you can apply the same logic for Nullable and Generic types (i.e. DateTime?, int?, Dictionary, etc.).
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Linq; using System.Runtime.Serialization; using System.Xml; namespace www.aspnet4you.com { [Serializable] public class Response : ISerializable { public Response(){} /// <summary> /// Deserialization Constructor /// </summary> /// <param name="info"></param> /// <param name="context"></param> public Response(SerializationInfo info, StreamingContext context) { try { AnyAsXmlString = new XElement("AnyAsXmlString"); AnyAsXmlString.Add(XElement.Parse((string)info.GetValue("AnyAsXmlString", typeof(string)))); } catch { AnyAsXmlString = null; } } /// <summary> /// AnyAsXmlString property is used to pass XML data between client and server. XElement is not /// serializable and Xml is converted to string when serialized (must to work with non .net clients). /// You can pass any data from server but client has to know the schema out-of-band. AnyAsXmlString /// is used for unknown or ever chaging data contract. /// </summary> public XElement AnyAsXmlString { get; set; } /// <summary> /// Custom Serialization is provided to avoid null reference during soap serialization. /// You can apply the same approach to serialize Nullable DateTime or Generics. /// Note: You must call GetObjectData on the base if you have inherited the class or you have to /// handle each property in your class if GetObjectData is not available at the base (i.e. objects in 3rd party dll). /// </summary> /// <param name="info"></param> /// <param name="context"></param> void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) { if (AnyAsXmlString != null) { info.AddValue("AnyAsXmlString", AnyAsXmlString.ToString(SaveOptions.None)); } } } }
Original posting at SoapUI:
http://www.soapui.org/forum/viewtopic.php?f=5&t=15831