Newtonsoft Bson Serializer

NuGet Package: NServiceBus.Newtonsoft.Json (3-pre)
This page targets a pre-release version. Pre-releases are subject to change and samples are not guaranteed to be fully functional.

This sample uses the Newtonsoft serializer NServiceBus.Newtonsoft.Json and configures it to use BSON.

Configuring to use NServiceBus.Newtonsoft.Json

var endpointConfiguration = new EndpointConfiguration("Samples.Serialization.ExternalBson");
var serialization = endpointConfiguration.UseSerialization<NewtonsoftSerializer>();
serialization.ReaderCreator(stream => new BsonDataReader(stream));
serialization.WriterCreator(stream => new BsonDataWriter(stream));

Diagnostic mutator

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

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

    public Task MutateIncoming(MutateIncomingTransportMessageContext context)
        var bodyAsString = Encoding.UTF8
        log.Info("Serialized Message Body:");
        return Task.CompletedTask;

Register the mutator.

endpointConfiguration.RegisterMessageMutator(new MessageBodyWriter());

Sending a message

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
await endpointInstance.SendLocal(message)


Related Articles

  • Serialization
    Information about how messages are serialized and deserialized on a transport.

Last modified