Has anyone seen a library that tests WCF DataContracts? The motivation behind asking this is that I just found a bug in my app that resulted from my not annotating a propert
It's simple enough to use DataContractSerializer
to serialise your object to a MemoryStream
, then deserialise it back into existence as a new instance.
Here's a class that demonstrates this round-trip serialisation:
public static class WcfTestHelper
{
///
/// Uses a to serialise the object into
/// memory, then deserialise it again and return the result. This is useful
/// in tests to validate that your object is serialisable, and that it
/// serialises correctly.
///
public static T DataContractSerializationRoundTrip(T obj)
{
return DataContractSerializationRoundTrip(obj, null);
}
///
/// Uses a to serialise the object into
/// memory, then deserialise it again and return the result. This is useful
/// in tests to validate that your object is serialisable, and that it
/// serialises correctly.
///
public static T DataContractSerializationRoundTrip(T obj,
IEnumerable knownTypes)
{
var serializer = new DataContractSerializer(obj.GetType(), knownTypes);
var memoryStream = new MemoryStream();
serializer.WriteObject(memoryStream, obj);
memoryStream.Position = 0;
obj = (T)serializer.ReadObject(memoryStream);
return obj;
}
}
Two tasks that you are responsible for:
Equals/GetHashCode
implementation, then that might already be done for you. Again you might try using a generic reflective comparer, but this mightn't be completely reliable.