The architecture in my application is somewhat like this
MainUI------->WCF------->BLL------->DAL
I am using Entity Framework 4.0 and .Net Framework 4.0.
My data access layer returns PoCo object which is getting serialized and deserialized while transferring the object to and from.
Now when WCF is returning the object before it gets serialized it is fine, just as I expected but when it gets deserialized it sometimes misses some properties(Navigational Properties) of my custom objects, not all the time but sometimes. Especially when i send List of custom objects over the wire. It returns the values for the single object all the time.
For the record, I am using DataContract Serializer.
I want some insight of this Serialization/Deserialization process. And I also want to view the serialized object and the exact points where an object is getting serialized and deserialized.
Serialization is the process of converting the state of an object into a form that can be persisted or transported. The complement of serialization is deserialization, which converts a stream into an object. Together, these processes allow data to be stored and transferred.
Serialization is the process of converting an object into a stream of bytes to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.
Types of Serialization Serialization can be of the following types: 1. Binary Serialization 2. SOAP Serialization 3. XML Serialization Binary Serialization: Binary serialization is a mechanism which writes the data to the output stream such that it can be used to re-construct the object automatically.
The advantage of serialization is the ability to transmit data across the network in a cross-platform-compatible format, as well as saving it in a persistent or non-persistent storage medium in a non-proprietary format.
I don't believe there is an easy way to Debug Serialization but in fact, there is no magic : Serialization is a fair simple process and you can do it by yourself.
For the record, I am using DataContract Serializer.
Here is the code to Serialize/Deserialize
public static string Serialize(object obj) {
using(MemoryStream memoryStream = new MemoryStream())
using(StreamReader reader = new StreamReader(memoryStream)) {
DataContractSerializer serializer = new DataContractSerializer(obj.GetType());
serializer.WriteObject(memoryStream, obj);
memoryStream.Position = 0;
return reader.ReadToEnd();
}
}
public static object Deserialize(string xml, Type toType) {
using(Stream stream = new MemoryStream()) {
byte[] data = System.Text.Encoding.UTF8.GetBytes(xml);
stream.Write(data, 0, data.Length);
stream.Position = 0;
DataContractSerializer deserializer = new DataContractSerializer(toType);
return deserializer.ReadObject(stream);
}
}
it sometimes misses some properties
Basically if something is wrong during the Serialization process, the serializer will throw a SerializationException (with details). In your case (property still empty or equals to default), it sounds like you have forgotten some attributes.
Well, it's not easy to help you a bit more without any piece of code, but just be aware of datacontractserializer features (see here).
Especially when i send List of custom objects over the wire. It returns the values for the single object all the time.
Try to reproduce it and write a unit test for that. There are no random errors, but just very specific scenarios that lead to errors.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With