Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NetDataContractSerializer vs DataContractSerializer

We have an application with objects that we'd like to keep serialized in the database.
Currently we're using NetDataContractSerializer, but recently found out that due to the .Net types information involved, it creates huge files which means slow application, even for basic objects.
We're considering switching to DataContractSerializer instead, and I've been looking for a nice comparison of the two but didn't find one.

  • What's the difference in sizes between the objects created by the two?
  • Is there a big performance difference?
  • Is any of them problematic when I have an object that contains a List<X>, where X is inherited by multiple other objects, so that the list contains many different types at runtime? (I was told that DataContractSerializer can be given KnownTypes attributes, but this means more dependencies in the code. Is there a way to make DataContractSerializer know all that types in my solution?)

Thanks.

like image 405
Noich Avatar asked Oct 25 '11 14:10

Noich


People also ask

Which namespace is used for serialization in WCF?

DataContractSerializer as the Default By default WCF uses the DataContractSerializer class to serialize data types.

What serializer does WCF use?

Windows Communication Foundation (WCF) uses the DataContractSerializer as its default serialization engine to convert data into XML and to convert XML back into data.


1 Answers

  1. NetDataContractSerializer (NetDCS) writes the type information for all objects, while the DataContractSerializer (DCS) only writes those which need to (i.e., if you declare a member as type B, and the actual value of the member when being serialized is of type D, where D is a derived type from B, so there is a lot less "noise" in a DCS-serialized type.
  2. Not really, but you should try both with your scenario to see if it will affect you
  3. You need to use known types in the DCS, but you can use a DataContractResolver if you don't want to work with known types. An example of such a resolver can be found in Youssef Massaoui's blog, and some more information on the resolver itself can be found at my post on WCF extensibility at http://blogs.msdn.com/b/carlosfigueira/archive/2011/09/21/wcf-extensibility-data-contract-resolver.aspx.
like image 109
carlosfigueira Avatar answered Jan 04 '23 01:01

carlosfigueira