Xml Serializer

Project Hosting | Nuget: NServiceBus (Version: 6.x)

A custom written XML serializer.

Usage

Edit
endpointConfiguration.UseSerialization<XmlSerializer>();

Raw XML

In certain integration scenarios it may be necessary to bypass NServiceBus opinionated serialization format (essentially key/value pairs) and directly send custom XML structures over messaging. In order to do that declare one or multiple properties on the message contract as XDocument or XElement.

Message with XDocument

Edit
public class MessageWithXDocument :
    IMessage
{
    // name and casing must match the rootnode
    public XDocument nutrition { get; set; }
}

Payload with XDocument

Edit
<nutrition>
    <daily-values>
        <total-fat units='g'>65</total-fat>
        <saturated-fat units='g'>20</saturated-fat>
    </daily-values>
</nutrition>

Message with XElement

Edit
public class MessageWithXElement :
    IMessage
{
    // name and casing must match the rootnode
    public XElement nutrition { get; set; }
}

Payload with XElement

Edit
<nutrition>
    <daily-values>
        <total-fat units='g'>65</total-fat>
        <saturated-fat units='g'>20</saturated-fat>
    </daily-values>
</nutrition>

The caveat of this approach is that the serializer will wrap the data in an outer node being named after the name of the property. So in the examples above note the associated expected payloads.

To avoid that, for interoperability reasons, instruct the serializer to not wrap raw XML structures:

Edit
var serialization = endpointConfiguration.UseSerialization<XmlSerializer>();
serialization.DontWrapRawXml();
The name of the property on the message must exactly match the name of the root node in the XML structure in order to be able to correctly deserialize the no longer wrapped content.

Caveats

XML Attributes

The XML serializer in NServiceBus is a custom implementation. As such it does not support the standard .NET XML Attributes.

Unsupported Types

  • HashTable
  • Types with non-default constructors. So types must have a public constructor with no parameters.
  • ArrayList

Samples


Last modified