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.
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.
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.