Should I Still Use BinaryFormatter for Simple Serialization in .NET 4.0?
Asked Answered
B

2

8

I am developing a master-slave style application. The master application will send state data to the slave(s) to process and display at some constant rate. The state data is wrapped up into a single class that contains many fields. These field types consist of primitives, classes, interfaces, lists of interfaces, and so on. All the types are either BCL or custom types, so the the custom types can be modified if necessary. Both the master and and slave applications will be .NET 4.0. I am not concerned with serialization versioning as the master and slave applications will be delivered as a pair.

I need a "quick" way to serialize the state data on the master and deserialize it on the slaves. When I say "quick", I am more talking about development time (but processing time could be a factor if the solution was terrible). However, the master and slaves will be distributed over a WAN, so some level of compactness would be nice also.

For a quick solution, I am currently thinking about simply using BinaryFormatter and then compressing the stream with GZipStream. Is this the way to go for .NET 4.0?

Busiek answered 12/3, 2011 at 18:50 Comment(2)
BinaryFormatter is fine, no need to drag around a 3rd party solution for your needs and its dead-simple to use. Don't use the GZipStream, it only adds overhead. Memory bandwidth is too high to let it pay off.Trickle
@Hans: He's talking about sending the data over a WAN (which probably means bandwidth less than 10Mbps). I would think GZipStream is almost mandatory for this application.Lassitude
J
5

If speed-of-development is the key (especially since you have interfaces etc, which you need to configure appropriately for some serializers) then maybe. Just remember to mark any events as:

[field:NonSerialized]

On every other measure (CPU performance, bandwidth, robustness as you version, interoperability, cost of maintenance, etc) I would choose other formats :)

Here's a selection profiled:

Performance Tests of Serializations used by WCF Bindings

Perhaps not a surprise (since I wrote it), but I lean towards protobuf-net...

Jocelin answered 12/3, 2011 at 18:54 Comment(3)
Great point on marking events with [NonSerialized]. I didn't think about that.Busiek
The perf tests of protobuf-net are very impressive. Will your library work for my case? I have to serialize interfaces and lists of interfaces. Some of these interfaces represent immutable objects; i.e. they only have getter properties, and the implementation classes have private setters.Busiek
@Busiek until about a week ago it would have struggled with that; last weekend I added a feature that would enable that, by storing the underlying type's details - an ugly answer to a very common request. See my blog for more.Jocelin
E
2

Have a look at Protocol Buffers here: Fast and compact object serialization in .NET

Edger answered 12/3, 2011 at 19:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.