XML Serializer sample

Component: Xml Serializer
NuGet Package: NServiceBus (8-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 default NServiceBus XML serializer in the core.

Configuring to use XML

var endpointConfiguration = new EndpointConfiguration("Samples.Serialization.Xml");
// this is optional since Xml is the default serializer
// register the mutator so the the message on the wire is written
endpointConfiguration.RegisterMessageMutator(new MessageBodyWriter());

Diagnostic mutator

A helper that will log the contents of any incoming message:

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

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

Sending the 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)


<?xml version="1.0" ?>
<CreateOrder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.net/XmlSample">

Related Articles

Last modified