Bond Serializer

Project Hosting
NuGet Package NServiceBus.Bond (1.3-pre) | License
This is a community run project. License and support are independent of Particular Software.
Target NServiceBus Version: 7.x
This page targets a pre-release version and is subject to change prior to the final release.

Serializes messages with Microsoft Bond.

Bond is a cross-platform framework for working with schematized data. It supports cross-language de/serialization and powerful generic mechanisms for efficiently manipulating data. Bond is broadly used at Microsoft in high scale services.

Usage

endpointConfiguration.UseSerialization<BondSerializer>();
This serializer does not support Messages defined as Interfaces.If an explicit interface is send an exception will be thrown with the following message:

Interface based message are not supported.
Create a class that implements the desired interface

Instead use a public class with the same contract as the interface. The class can optionally implement any required interfaces.

SerializationDelegates

Customizes the cached delegates that serialize and deserialize message types. This is an optional setting.

The default serialization delegates are as follows.

var serialization = endpointConfiguration.UseSerialization<BondSerializer>();
serialization.SerializationDelegates(
    sertializationDelegatesBuilder: messageType =>
    {
        var item = SerializerCache.GetSerializer(messageType);
        return new SerializationDelegates(
            serialize: (buffer, message) =>
            {
                var writer = new CompactBinaryWriter<OutputBuffer>(buffer);
                item.Serializer.Serialize(message, writer);
            },
            deserialize: buffer =>
            {
                var reader = new CompactBinaryReader<InputBuffer>(buffer);
                return item.Deserializer.Deserialize(reader);
            });
    });

The serializers are cached as per the Bond performance guidance.

static class SerializerCache
{
    static ConcurrentDictionary<Type, Item> cache = new ConcurrentDictionary<Type, Item>();

    public static Item GetSerializer(Type messageType)
    {
        return cache.GetOrAdd(messageType,
            type => new Item
            (
                new Serializer<CompactBinaryWriter<OutputBuffer>>(type),
                new Deserializer<CompactBinaryReader<InputBuffer>>(type)
            ));
    }

    public class Item
    {
        public readonly Serializer<CompactBinaryWriter<OutputBuffer>> Serializer;
        public readonly Deserializer<CompactBinaryReader<InputBuffer>> Deserializer;

        public Item(
            Serializer<CompactBinaryWriter<OutputBuffer>> serializer,
            Deserializer<CompactBinaryReader<InputBuffer>> deserializer)
        {
            Serializer = serializer;
            Deserializer = deserializer;
        }
    }
}

Custom Content Key

When using additional deserializers or transitioning between different versions of the same serializer it can be helpful to take explicit control over the content type a serializer passes to NServiceBus (to be used for the ContentType header).

var serialization = endpointConfiguration.UseSerialization<BondSerializer>();
serialization.ContentTypeKey("custom-key");

Samples


Last modified