Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serialization internals in .Net

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.

like image 249
Manvinder Avatar asked May 01 '13 11:05

Manvinder


People also ask

What is .NET serialization?

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.

What is serialization in .NET with example?

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.

How many types of serialization are there in C#?

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.

How does serialization affect .NET programming?

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.


1 Answers

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.

like image 189
Cybermaxs Avatar answered Oct 08 '22 01:10

Cybermaxs