Newtonsoft Bson Serializer

Component: Newtonsoft Serializer
NuGet NServiceBus.Newtonsoft.Json (1.x)
Target NServiceBus Version: 6.x

NServiceBus.Newtonsoft.Json

This sample uses the Newtonsoft serializer NServiceBus.Newtonsoft.Json to provide full access to the Newtonsoft Json.net API.

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));

// register the mutator so the the message on the wire is written
endpointConfiguration.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<IMutateIncomingTransportMessages>();

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

}

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
        },
    }
};
await endpointInstance.SendLocal(message)
    .ConfigureAwait(false);

Related Articles


Last modified