Brokered Message Creation

Component: Azure Service Bus Transport (Legacy)
NuGet Package NServiceBus.Azure.Transports.WindowsAzureServiceBus (6.x)
Target NServiceBus Version: 5.x
Standard support for version 5.x of NServiceBus has expired. For more information see our Support Policy.
This page refers to the legacy Azure Service Bus transport, which is rendered obsolete by the Azure Service Bus transport built to target both .NET Framework and .NET Core. All new projects should use the new Azure Service Bus transport.

This document describes how to modify brokered message creation logic in order to set up a native integration between NServiceBus and non-NServiceBus endpoints.

BrokeredMessage conventions

Brokered Message body format

The Azure Service Bus API allows the construction of a BrokeredMessage body from a stream or an object that will then be serialized into a byte array.

Both the sender (native or NServiceBus) and the receiver must have a consistent convention for the communication the message body.

By default, the BrokeredMessage body is assumed to be a byte array to remain backward compatible with previous versions of the transport. It is recommended that the body can be retrieved using Stream for new implementations, as it incurs less of a memory overhead during serialization. It is especially useful for scenarios, such as native integration, as most non .NET ASB SDK's only support the Stream body format.

The SupportedBrokeredMessageBodyTypes.Stream body format is recommended, but this mode is currently NOT compatible with ServiceControl. A ServiceControl transport adapter is required in order to leverage both.

To specify how the BrokeredMessage body is stored and retrieved, override the default conventions.

Outgoing message:

BrokeredMessageBodyConversion.InjectBody =
    bytes =>
    {
        var messageAsStream = new MemoryStream(bytes);
        return new BrokeredMessage(messageAsStream);
    };

Incoming message:

BrokeredMessageBodyConversion.ExtractBody =
    brokeredMessage =>
    {
        using (var stream = new MemoryStream())
        using (var body = brokeredMessage.GetBody<Stream>())
        {
            body.CopyTo(stream);
            return stream.ToArray();
        }
    };

Brokered Message content serialization

The Azure Service Bus transport is using the JSON serializer by default. Therefore, the message sent by a native sender needs to be valid JSON.

{
    "Content": "Hello from native sender",
    "SentOnUtc": "2015-10-27T20:47:27.4682716Z" 
}

If the application uses a different serialization for message content, then it can be configured as follows.

busConfiguration.UseSerialization<XmlSerializer>();

If the message content is in an unsupported or proprietary format, then the application will have to provide a custom serializer

Message type detection

The native message must allow NServiceBus to detect the message type either via the headers or the message payload.

Related Articles


Last modified