WCF UnitTests series tests and this XmlWriter do not support base64 encoded data. Which writer should I use?

I am writing some unit tests that serialize and deserialize all our types that can cross the WCF border to prove that all properties will cross over.

I hit a little with bytes [].

[DataContract(IsReference=true)] public class BinaryDataObject { [DataMember] public byte[] Data { get; set; } } 

When I run this object through testing, I get a System.NotSupportedException: This XmlWriter does not support base64 encoded data .

Here is my Serialization method:

 public static XDocument Serialize(object source) { XDocument target = new XDocument(); using (System.Xml.XmlWriter writer = target.CreateWriter()) { DataContractSerializer s = new DataContractSerializer(source.GetType()); s.WriteObject(writer, source); } return target; } 

It seems to me that my serialization method should be wrong - WCF probably does not use XDocument instances and may not use System.Xml.XmlWriter instances.

Which Writer uses WCF by default? I would like to use instances of this type in my test.

+4
source share
1 answer

Using my ninja reflector skills, it seems like it uses some internal types: subclasses of XmlDictionaryWriter . Rewrite your Serialize method as such:

  public static XDocument Serialize(object source) { XDocument target; using (System.IO.MemoryStream stream = new System.IO.MemoryStream()) using (System.Xml.XmlWriter writer = XmlDictionaryWriter.CreateTextWriter(stream)) { DataContractSerializer s = new DataContractSerializer(source.GetType()); s.WriteObject(writer, source); writer.Flush(); stream.Position = 0; target = XDocument.Load(stream); } return target; } 

and everything needs to be fixed.

+5
source

All Articles