• Stars
    star
    379
  • Rank 109,609 (Top 3 %)
  • Language
    C#
  • License
    Apache License 2.0
  • Created almost 9 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Binary serializer for POCO objects

[Archived]

Due to how Wire handles type information on the wire, malicious payloads can be passed. e.g. using a surrogate on the sender end, an attacker can pass information about a different type for the receiving end. And by doing so allowing the serializer to create any type on the deserializing end.

This is the same issue that exists for BinaryFormatter https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2300?view=vs-2019

Any future forks or derivates of Wire will have to account for this.

Security advisory: https://github.com/AsynkronIT/Wire/security/advisories/GHSA-hpw7-3vq3-mmv6

Wire

A high performance polymorphic serializer for the .NET framework.

Wire is still in beta and may have breaking changes to both the API and serialization format on the road to 1.0

Polymorphic serializations

Wire was designed to safely transfer messages in distributed systems, for example service bus or actor model based systems. In message based systems, it is common to receive different types of messages and apply pattern matching over those messages. If the messages does not carry over all the relevant type information to the receiveing side, the message might no longer match exactly what your system expect.

Consilder the following case:

public class Envelope
{
     //untyped property
     public object Payload {get;set;}
     //other properties..
     ...
}

...

var envelope = new Envelope { Payload = (float)1.2 };

If you for example are using a Json based serializer, it is very likely that the value 1.2 will be deserialized as a double as Json has no way to describe the type of the decimal value. While if you use some sort of binary serializer like Google Protobuf, all messages needs to be designed with a strict contract up front. Wire solves this by encoding a manifest for each value - a single byte prefix for primitive values, and fully qualified assembly names for complex types.

Surrogates

Sometimes, you might have objects that simply can't be serialized in a safe way, the object might be contextual in some way. Wire can solve those problems using "Surrogates", surrogates are a way to translate an object from and back to the context bound representation.

var surrogate = Surrogate.Create<IMyContextualInterface,IMySurrogate>(original => original.ToSurrogate(), surrogate => surrogate.Restore(someContext));
var options = new SerializerOptions(surrogates: new [] { surrogate });
var serializer = new Serializer(options);

This is essential for frameworks like Akka.NET where we need to be able to resolve live Actor References in the deserializing system.

Version Tolerance

Wire has been designed to work in multiple modes in terms of version tolerance vs. performance.

  1. Pre Register Types, when using "Pre registered types", Wire will only emit a type ID in the output stream. This results in the best performance, but is also fragile if different clients have different versions of the contract types.
  2. Non Versioned, this is largely the same as the above, but the serializer does not need to know about your types up front. it will embed the fully qualified typename in the outputstream. this results in a larger payload and some performance overhead.
  3. Versioned, in this mode, Wire will emit both type names and field information in the output stream. This allows systems to have slightly different versions of the contract types where some fields may have been added or removed.

Durable persistence and version tolerance

Wire has been designed as a wire format, point to point for soft realtime scenarios. If you need a format that is durable for persistence over time. e.g. EventSourcing or for message queues, then Protobuf, Thrift, Flatbuffers or MS Bond will be a better choise as those formats have been designed for true versiom tolerance.

Performance

Wire has been designed with a performance first mindset. It is not the most important aspect of Wire, Surrogates and polymorphism is more critical for what we want to solve. But even with it's rich featureset, Wire performs extremely well.

Wire - preregister types
   Serialize                      312 ms
   Deserialize                    261 ms
   Size                           38 bytes
   Total                          573 ms
Wire - no version data
   Serialize                      327 ms
   Deserialize                    354 ms
   Size                           73 bytes
   Total                          681 ms
Wire - preserve object refs
   Serialize                      400 ms
   Deserialize                    369 ms
   Size                           73 bytes
   Total                          769 ms
MS Bond
   Serialize                      429 ms
   Deserialize                    404 ms
   Size                           50 bytes
   Total                          833 ms
Wire - version tolerant
   Serialize                      423 ms
   Deserialize                    674 ms
   Size                           195 bytes
   Total                          1097 ms
Protobuf.NET
   Serialize                      638 ms
   Deserialize                    721 ms
   Size                           42 bytes
   Total                          1359 ms
Jil
   Serialize                      1448 ms
   Deserialize                    714 ms
   Size                           123 bytes
   Total                          2162 ms
Net Serializer
   Serialize                      1289 ms
   Deserialize                    1113 ms
   Size                           39 bytes
   Total                          2402 ms
Json.NET
   Serialize                      3767 ms
   Deserialize                    5936 ms
   Size                           187 bytes
   Total                          9703 ms
Binary formatter
   Serialize                      10784 ms
   Deserialize                    11374 ms
   Size                           362 bytes
   Total                          22158 ms

This test was run using the following object definition:

public class Poco
{
    public string StringProp { get; set; }      //using the text "hello"
    public int IntProp { get; set; }            //123
    public Guid GuidProp { get; set; }          //Guid.NewGuid()
    public DateTime DateProp { get; set; }      //DateTime.Now
}

Big disclaimer: The above results change drastically depending on your contracts, e.g. using smaller messages favor both NetSerializer and Jil. There is no "best" or "fastest" serializer, it all depends on context and requirements.

More Repositories

1

protoactor-go

Proto Actor - Ultra fast distributed actors for Go, C# and Java/Kotlin
Go
4,895
star
2

protoactor-dotnet

Proto Actor - Ultra fast distributed actors for Go, C# and Java/Kotlin
C#
1,670
star
3

protoactor-kotlin

Ultra-fast distributed cross-platform actor framework
Kotlin
215
star
4

TraceLens

C#
132
star
5

realtimemap-dotnet

A showcase for Proto.Actor - an ultra-fast distributed actors solution for Go, C#, and Java/Kotlin.
C#
101
star
6

protoactor-python

Proto Actor - Ultra fast distributed actors
Python
80
star
7

realtimemap-go

Go
55
star
8

protoactor-bootcamp

C#
53
star
9

protoactor-js

TypeScript
37
star
10

dotdock

Generate dockerfiles for .NET Solutions
C#
21
star
11

protoactor-website

JavaScript
13
star
12

AwesomeActors

10
star
13

protoactor-contracts

Cross-platform contracts for Proto.Actor
Protocol Buffer
7
star
14

CallMeLater

Send delayed/scheduled HTTP requests
Go
7
star
15

protoactor-grains-tutorial

Getting Started With Grains / Virtual Actors Tutorial (.NET)
C#
7
star
16

gofun

Go
6
star
17

Akka.OpenTelemetry

C#
4
star
18

protoactor-cli

Interactive CLI for Proto.Actor
4
star
19

protoactor-cluster-dashboard

HTML
3
star
20

protoactor-dotnet-contrib

Contributions for ProtoActor .NET
C#
3
star
21

protoactor-workshop

Go
2
star
22

goring

Go
2
star
23

docker-protoc

protoc packaged in docker
Dockerfile
2
star
24

gam-web

CSS
2
star
25

IsExternalInit

C#
1
star
26

KafkaActors

Virtual Actors built on Kafka
1
star
27

MongoToPostgres

Convert MongoDB JSON Queries to Postgres JSON SQL
C#
1
star
28

website

HTML
1
star
29

goconsole

Go
1
star
30

TinyTypes

Tiny functional programming library for C#
C#
1
star
31

gotimeout

Cachable timeouts for Go
Go
1
star