Serialization internals in .Net
Asked Answered
P

2

6

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.

Pascual answered 1/5, 2013 at 11:59 Comment(0)
D
1

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.

Docila answered 7/5, 2013 at 7:37 Comment(0)
C
0

You can use IMessageInspector extension point to see what message shape looks like which will include your object graph. See here for implementing IMessageInspector.

Another option is to implement OnDeserializing attribute to gain access to serializing process.

On top of that you could turn on WCF tracing and capture message details as they cross the boundaries.

Centigram answered 8/5, 2013 at 20:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.