Version Tolerant Binary Serialization With Microsoft Bond In C# (2024)

Version Tolerant Binary Serialization With Microsoft Bond In C# (1)

by Jorge Candeias on

You’ve been there too. A month ago you wrapped up your first release. You were happy and went for a beer with your workmates. The next month things didn’t go so well. As you start integration testing again, you realize something has gone wrong. You dig down, and discover your app had serialized some useful data to disk not so long ago. Yet, as the new version does its thing, it crashes with a vengeance, when it tries to get that data back from the same place. As you sigh, and realize there won’t be any beer that night, you notice that those serialized bits of data have changed schema between deployments. You think - I do want the app to re-use that data without breaking… So how on Earth do I get around this?

The answer to this is remarkably simple - if your serialization code needs to tolerate versions… Then make it version tolerant!

Version Tolerant Binary Serialization With Microsoft Bond In C# (2)

Now there are many ways of making serialization version tolerant, and they all depend on how exactly you are serializing your data.

A direct approach can be to just JSON.NET-the-lot and go with that. And that’s fine, it works and pays the bills. Happy camping, Homer.

But what if you care about the milliseconds? What if you do want the brute speed and small size of binary serialization… and the benefits of text based version tolerance?

Well that’s a what version tolerant binary serializer is there for. (Again, the answer is so simple, it should be a criminal offense).

Below I’ll show you how to setup and use the Microsoft Bond serializer, so you get the best of both worlds.

Version Tolerant Binary Serialization With Microsoft Bond In C# (3)

My name is Bond… Microsoft Bond. Binary Martini… Shaken, not shifted.

Bond is Microsoft’s equivalent of Google’s Protocol Buffers and they both share the same traits:

  • Some annoying platform independent script compilation (though Visual Studio makes it a lot better) - this is in exchange for…
  • Fast binary (de)serialization by default. Bond uses a layered approach so you can mix and match features. The more you use, the more you pay, but it goes the other way too.
  • Compact binary format.
  • Platform and language agnostic - well kinda.

You can find the full sales pitch here.

Anyway, onwards to the how-to:

Install Bond.CSharp

The first step is to go grab the Bond.CSharp NuGet package.

Version Tolerant Binary Serialization With Microsoft Bond In C# (4)

This package will bring the Bond dependency lot with it and make life easier for you. If for some reason you need to micromanage your dependencies, you can also go with the several other Bond.* packages.

Restart Visual Studio

The Bond Package will activate a new Custom Build option in Visual Studio.You need to restart the IDE for this to take effect.

Create a Bond File

Now create a dot bond file to hold your serializable types. And don’t shake your head now, stick with me here.

Version Tolerant Binary Serialization With Microsoft Bond In C# (5)

Bond shares the same necessary evil of Protocol Buffers. As it is a platform-independant serializer, you need to specify its types in a platform-independant way. Bond needs to know what types actually are, regardless of the platform in use. One platform may use Big Endian encoding while another uses Little Endian. One platform may encode strings with a length prefix while another platform uses an end marker. Who knows? Yet you still want both platform to speak to each other, even in high speed binary. Both Bond and Protobuf’s answer to this is to use their own uniform types and translate as needed for each different platform.

Set the file’s Build Action to BondCodegen</h2>

Version Tolerant Binary Serialization With Microsoft Bond In C# (6)

This will let Bond do some magic in the background and generate C# classes for you that mirror the types you wrote. You can also compile these bond files with the bond compiler, but I won’t dive into that here.

Write Some Code To Save

This will serialize an instance of that SomeBondClass to disk - but you can also send the bytes via the pipe, of course.

Version Tolerant Binary Serialization With Microsoft Bond In C# (7)

Note the lack of the using clauses. That surprised me at first, streams being all IDisposable and all. Not so much with Bond, it cleans after itself. If only my dog did that as well.

Check That File Up

Version Tolerant Binary Serialization With Microsoft Bond In C# (8)

This binary file stands at a measly 15 bytes. Serialize this tiny same class as JSON and you end up with 50 bytes. You are using only 30% of bytes as you would with JSON. Won’t even bother with XML at this point.

Write Some Code To Load

Version Tolerant Binary Serialization With Microsoft Bond In C# (9)

This will read the serialized data back into a class for you. Here’s the result:

Version Tolerant Binary Serialization With Microsoft Bond In C# (10)

As expected.

Mess It Up

Okay, this is all nice but we haven’t made of use of any version tolerance whatsoever. So let’s make it so and break things up.

Introduce a change into your Bond class:

Version Tolerant Binary Serialization With Microsoft Bond In C# (11)

I just added a new property here for show, but do whatever you want.

Read It Back Again

This:

Version Tolerant Binary Serialization With Microsoft Bond In C# (12)

Outputs this:

Version Tolerant Binary Serialization With Microsoft Bond In C# (13)

Which is the behavior you’d get with say, JSON.NET - though for a much smaller file size, which is ideal for piping over or persisting to disk.

That’s It

If you want to dig in further, go check out the project at GibHub or the full manual at GitHub. Because GitHub.

I’m growing fond of Bond over time as another useful tool for implementing real-time systems. Anything that helps grab that extra millisecond helps. I may even start drinking Martini again.

Till next time.

Version Tolerant Binary Serialization With Microsoft Bond In C# (2024)

References

Top Articles
Nakuru County Assembly Members Profiles – County Assembly Of Nakuru Official Website
Emirates Skywards Meilen sammeln - Die 14 besten Möglichkeiten
Creepshotorg
Safety Jackpot Login
Cintas Pay Bill
Enrique Espinosa Melendez Obituary
Botanist Workbench Rs3
Cumberland Maryland Craigslist
Hallowed Sepulchre Instances &amp; More
CSC error CS0006: Metadata file 'SonarAnalyzer.dll' could not be found
Hover Racer Drive Watchdocumentaries
Clairememory Scam
Select Truck Greensboro
W303 Tarkov
Dusk
New Mexico Craigslist Cars And Trucks - By Owner
Diablo 3 Metascore
Used Drum Kits Ebay
Viha Email Login
Kvta Ventura News
Slope Tyrones Unblocked Games
Mikayla Campinos Laek: The Rising Star Of Social Media
Gayla Glenn Harris County Texas Update
Where Is George The Pet Collector
How To Tighten Lug Nuts Properly (Torque Specs) | TireGrades
Coindraw App
Cal State Fullerton Titan Online
Ncal Kaiser Online Pay
Visit the UK as a Standard Visitor
Blue Beetle Movie Tickets and Showtimes Near Me | Regal
Gets Less Antsy Crossword Clue
Culver's of Whitewater, WI - W Main St
Craigslist Lakeside Az
Nba Props Covers
What Is A K 56 Pink Pill?
RECAP: Resilient Football rallies to claim rollercoaster 24-21 victory over Clarion - Shippensburg University Athletics
manhattan cars & trucks - by owner - craigslist
Immobiliare di Felice| Appartamento | Appartamento in vendita Porto San
Anderson Tribute Center Hood River
Shipping Container Storage Containers 40'HCs - general for sale - by dealer - craigslist
Wilson Tire And Auto Service Gambrills Photos
Comanche Or Crow Crossword Clue
Fatal Accident In Nashville Tn Today
Streameast Io Soccer
Dicks Mear Me
Craigslist Sparta Nj
1990 cold case: Who killed Cheryl Henry and Andy Atkinson on Lovers Lane in west Houston?
116 Cubic Inches To Cc
Zadruga Elita 7 Live - Zadruga Elita 8 Uživo HD Emitirani Sat Putem Interneta
La Fitness Oxford Valley Class Schedule
Latest Posts
Article information

Author: Msgr. Refugio Daniel

Last Updated:

Views: 5893

Rating: 4.3 / 5 (54 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Msgr. Refugio Daniel

Birthday: 1999-09-15

Address: 8416 Beatty Center, Derekfort, VA 72092-0500

Phone: +6838967160603

Job: Mining Executive

Hobby: Woodworking, Knitting, Fishing, Coffee roasting, Kayaking, Horseback riding, Kite flying

Introduction: My name is Msgr. Refugio Daniel, I am a fine, precious, encouraging, calm, glamorous, vivacious, friendly person who loves writing and wants to share my knowledge and understanding with you.