Native Integration with ASQ

This page targets a pre-release version. Pre-releases are subject to change and samples are not guaranteed to be fully functional.

This document describes how to consume messages from and send messages to non-NServiceBus endpoints via Azure Storage Queues in integration scenarios.

Sending native messages

Sending native messages can be accomplished by sending a message with a JSON serialized payload using the QueueClient. Refer to the sample for more information.

Custom envelope wrapper

Azure Storage Queues lacks native header support. NServiceBus solves this by wrapping headers and message body in a custom envelope structure. This envelope is serialized using the configured serializer for the endpoint before being sent.

Creating this envelope can cause unneeded complexity should headers not be needed for example in native integration scenarios. For this reason NServiceBus.Azure.Transports.WindowsAzureStorageQueues 7.1 and above support configuring a custom envelope unwrapper.

The snippet below shows custom unwrapping logic that enables both NServiceBus formatted and plain JSON messages to be consumed.

var transport = new AzureStorageQueueTransport("connection string")
{
    MessageUnwrapper = queueMessage =>
    {
        using (var stream = new MemoryStream(Convert.FromBase64String(queueMessage.MessageText)))
        using (var streamReader = new StreamReader(stream))
        using (var textReader = new JsonTextReader(streamReader))
        {
            //try deserialize to a NServiceBus envelope first
            var wrapper = jsonSerializer.Deserialize<MessageWrapper>(textReader);

            if (wrapper.Id != null)
            {
                //this was a envelope message
                return wrapper;
            }

            //this was a native message just return the body as is with no headers
            return new MessageWrapper
            {
                Id = queueMessage.MessageId,
                Headers = new Dictionary<string, string>(),
                Body = Convert.FromBase64String(queueMessage.MessageText)
            };
        }
    }
};

endpointConfiguration.UseTransport(transport);
This feature is currently NOT compatible with ServiceControl. A ServiceControl transport adapter is required in order to leverage both.

Samples


Last modified