This sample uses the Newtonsoft serializer NServiceBus.Newtonsoft.Json and configures it to use BSON.
Configuration
var serialization = endpointConfiguration.UseSerialization<NewtonsoftJsonSerializer>();
serialization.ContentTypeKey("application/bson");
serialization.ReaderCreator(stream => new BsonDataReader(stream));
serialization.WriterCreator(stream => new BsonDataWriter(stream));
Diagnostic mutator
This helper will write out the contents of any incoming message so that the serialization can be observed.
public class MessageBodyWriter :
IMutateIncomingTransportMessages
{
readonly static ILog log = LogManager.GetLogger<IMutateIncomingTransportMessages>();
public Task MutateIncoming(MutateIncomingTransportMessageContext context)
{
var bodyAsString = Encoding.UTF8
.GetString(context.Body.ToArray());
log.Info("Serialized Message Body:");
log.Info(bodyAsString);
return Task.CompletedTask;
}
}
This registers 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);