Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 547
dewald Avatar asked Mar 12 '11 18:03

dewald


People also ask

Why is BinaryFormatter insecure?

BinaryFormatter uses violates 2.), which is a huge security risk because it makes possible to run any code.

Why do we use serialization in asp net?

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.

What is SOAP serialization in C#?

In SOAP and BINARY serialization technique the state of the entire object is serialized into a stream of bytes. In cases where the object contains a reference to other objects, even those are serialized. This type of serialization is known as deep serialization.


1 Answers

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...

like image 100
Marc Gravell Avatar answered Sep 24 '22 05:09

Marc Gravell