Binary serialization in .NET Core
Asked Answered
T

5

5

I am working on a .NET Core project and I am trying to parse my List<T> to byte[]. Using the .NET Framework, we could have achieved the same by using BinaryFormatter, but at the time of writing this question it looks like Microsoft does not yet support it in .NET Core and no upcoming releases seem to do that.

Can anybody tell how to perform this serialization in .NET Core? Also, is binary serialization platform-dependent, and for such reason been deprecated in .NET Core?

Transgress answered 1/3, 2017 at 8:20 Comment(2)
Well it was generally flaky and error-prone, in my experience. (I thought it was coming back in netstandard2.0, but I could be wrong.) The "workaround" is to use a different serialization format - there are plenty to choose from.Cutwater
It is available.Clutter
L
4

You can use Binaron.Serializer - https://github.com/zachsaw/Binaron.Serializer There's no need to decorate your class with any attributes.

Disclaimer: I'm the author of Binaron.Serializer.

Lucilelucilia answered 1/2, 2020 at 0:43 Comment(3)
from a little searching, I have concluded that Binaron and MessagePack are the most promising for fast and simple C# binary serialization. But I have not found anything that compares those two directly. So it's hard to figure out which to go with. Any pointers?Ligniform
Does this deserialization requires default constructor in the class being serailized?Genus
It does require default ctor, for now, until I have some spare time to work on it further.Lucilelucilia
I
3

You can use MessagePack. The package is chosen as Package of the week in .Net blog.

Nuget command:

Install-Package MessagePack

You can also take a look into their source code and see how it is implemented in .net core.

Irmgard answered 15/3, 2017 at 11:19 Comment(0)
D
3

.NET Core 2.1 now includes a BinaryFormatter you can use for this.

You can find more details in this answer.

Deaconry answered 6/7, 2018 at 12:8 Comment(0)
S
3

BinaryFormatter is getting obsoleted in the upcoming .NET versions due to its security flaws.

It is basically safe only if both serialization and deserialization happens in the same process (which is not the case in most scenarios) so it has been decided to remove it from future versions.

Though the obsoletion document says that in .NET 8 the complete binary serialization infrastructure will be removed I still hope this can be somewhat influenced. I recently opened an issue to discuss the possible ways of making binary serialization (and any polymorphic serialization) safe: https://github.com/dotnet/runtime/issues/50909

But as the other answers also illustrate, there are many custom binary serializers you can choose from. @ZachSaw's Binarion or MessagePack are equally popular, and I also made my binary serializer public a few years ago (NuGet). It tries to address the security aspects and good performance (meaning both speed and size).

But frankly, when communicating between remote entities (including file and database sources), a vulnerable binary serializer never should be used. And even the speed of the slower text-based serializers will be still much faster than any network communication so their speed barely can be real bottleneck.

Sabina answered 9/4, 2021 at 11:8 Comment(0)
C
1

For payload size and performance you can try BOIS which focuses on packed data size and provides the best packing so far. It also supports .Net Core

https://github.com/salarcode/Bois

Cornelia answered 13/3, 2019 at 0:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.