Serialization inside .Net

The architecture in my application is somewhat similar to this

MainUI------->WCF------->BLL------->DAL 

I am using Entity Framework 4.0 and .Net Framework 4.0.

My data access level returns a PoCo object that becomes serialized and deserialized when the object is transferred to and from.

Now, when WCF returns an object before it is serialized, it is beautiful, as expected, but when it is deserialized, it sometimes misses some properties (navigation properties) of my custom objects not all the time, and sometimes. Especially when I send a list of custom objects by wire. It returns values ​​for one object all the time.

For recording, I use Serializer DataContract.

I want to get an idea of ​​this serialization / deserialization process. And I also want to view the serialized object and the exact points where the object becomes serialized and deserialized.

+6
source share
2 answers

I do not believe that there is an easy way to debug serialization, but there really is no magic: serialization is a simple process, and you can do it yourself.

For recording, I use Serializer DataContract.

Here is the Serialize / Deserialize code

  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); } } 

he sometimes skips some properties

Basically, if something is wrong during the Serialization process, the serializer will throw a SerializationException (with detailed information). In your case (the property is still empty or equal by default), it looks like you forgot some attributes.

Well, it’s not easy for you to help you a bit more without any part of the code, but just be aware of the functions of the datacontractserializer (see here ),

Especially when I send a list of custom objects by cable. This returns values ​​for one object all the time.

Try playing it and write unit test for this. There are no random errors, but just very specific scenarios leading to errors.

+1
source

You can use the IMessageInspector extension point to see how the message form looks, which will include the graphic. See here for the implementation of IMessageInspector .

Another option is to implement the OnDeserializing attribute to access the serialization process.

In addition, you can enable WCF tracing and view message data when it crosses borders.

0
source

Source: https://habr.com/ru/post/944036/


All Articles