BSON Serializer

Component: Json Serializer
NuGet NServiceBus (5.x)

This sample uses the BSON serializer in the core.

In Versions 6 and above the built in BSON serializer has been deprecated. The Newtonsoft serializer can be used as a replacement

Configuring to use BSON

var busConfiguration = new BusConfiguration();
busConfiguration.EndpointName("Samples.Serialization.Bson");
busConfiguration.UseSerialization<BsonSerializer>();
// register the mutator so the the message on the wire is written
busConfiguration.RegisterComponents(components =>
{
    components.ConfigureComponent<MessageBodyWriter>(DependencyLifecycle.InstancePerCall);
});

Diagnostic Mutator

A helper that will Write out the contents of any incoming message.

public class MessageBodyWriter :
    IMutateIncomingTransportMessages
{
    static ILog log = LogManager.GetLogger<MessageBodyWriter>();

    public void MutateIncoming(TransportMessage transportMessage)
    {
        var bodyAsString = Encoding.UTF8
            .GetString(transportMessage.Body);
        log.Info("Serialized Message Body:");
        Console.WriteLine(bodyAsString);
    }
}

The message send

var message = new CreateOrder
{
    OrderId = 9,
    Date = DateTime.Now,
    CustomerId = 12,
    OrderItems = new List<OrderItem>
    {
        new OrderItem
        {
            ItemId = 6,
            Quantity = 2
        },
        new OrderItem
        {
            ItemId = 5,
            Quantity = 4
        },
    }
};
bus.SendLocal(message);

Related Articles


Last modified