XML Serializer

Component: Xml Serializer | Nuget: NServiceBus (Version: 5.x)

This sample uses the default XML serializer in the core.

Configuring to use XML

Edit
var busConfiguration = new BusConfiguration();
busConfiguration.EndpointName("Samples.Serialization.Xml");
// this is optional since Xml is the default serializer
busConfiguration.UseSerialization<XmlSerializer>();
// 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.

Edit
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:");
        log.Info(bodyAsString);
    }
}

The message send

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

The Output

<?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">
	<OrderId>9</OrderId>
	<Date>2015-09-15T13:33:28.7186624+10:00</Date>
	<CustomerId>12</CustomerId>
	<OrderItems>
		<OrderItem>
			<ItemId>6</ItemId>
			<Quantity>2</Quantity>
		</OrderItem>
		<OrderItem>
			<ItemId>5</ItemId>
			<Quantity>4</Quantity>
		</OrderItem>
	</OrderItems>
</CreateOrder>

Related Articles


Last modified