Getting Started

Azure Blob Storage DataBus

NuGet Package: NServiceBus.DataBus.AzureBlobStorage (6.x)
Target Version: NServiceBus 9.x
  1. Start Azurite Emulator.
  2. Run the solution. Two console applications start.
  3. Find the Sender application by looking for the one with Sender in its path and press Enter in the window to send a message. A message has been sent is larger than the allowed 4MB by MSMQ. NServiceBus sends it as an attachment via Azure storage, allowing it to reach the Receiver application.

Code walk-through

This sample contains three projects:

  • Shared - A class library containing shared code including the message definition.
  • Sender - A console application responsible for sending the large message.
  • Receiver - A console application responsible for receiving the large message from Sender.

Shared project

Look at the Shared project:

public class MessageWithLargePayload :
    public DataBusProperty<byte[]> LargePayload { get; set; }
    public string Description { get; set; }

DataBusProperty<byte[]> is an NServiceBus data type that instructs NServiceBus to treat the LargePayload property as an attachment. It is not transported in the NServiceBus normal flow.

When sending a message using the NServiceBus Message attachments mechanism, the message's payload resides on Azure storage as a blob. Value assigned to the property while message is being transported is a special value that allows to reconnect message`s property with its original value once message is received. An inspected message in flight would look like the following:

	"Description":"This message contains a large payload that will be sent on the Azure data bus",

Key represents Azure storage blob name used to store message property original value.

The TimeToBeReceived attribute instructs the NServiceBus framework that it is allowed to clean the message after three minutes if it was not received by the receiver. The message payload will be removed from Azure storage after three minutes.

Configuring the DataBus location

Both the Sender and Receiver project need to share a common location to store large binary objects. This is done by specifying Azure storage connection string. This code instructs NServiceBus to use specified Azure storage account for the attachment.

var blobServiceClient = new BlobServiceClient("UseDevelopmentStorage=true");
var dataBus = endpointConfiguration.UseDataBus<AzureDataBus, SystemJsonDataBusSerializer>()

Attachment blobs will be found in databus container.

Sender project

The following sender project code sends the MessageWithLargePayload message, utilizing the NServiceBus attachment mechanism:

var message = new MessageWithLargePayload
    Description = "This message contains a large payload that will be sent on the Azure data bus",
    LargePayload = new DataBusProperty<byte[]>(new byte[1024 * 1024 * 5]) // 5MB
await messageSession.Send("Samples.AzureBlobStorageDataBus.Receiver", message);

Go to the Receiver project to see the receiving application.

Receiver project

Following is the receiving message handler:

public class MessageWithLargePayloadHandler :
    static ILog log = LogManager.GetLogger<MessageWithLargePayloadHandler>();

    public Task Handle(MessageWithLargePayload message, IMessageHandlerContext context)
        log.Info($"Message received. Description: '{message.Description}'. Size of payload property: {message.LargePayload.Value.Length} Bytes");
        return Task.CompletedTask;


Related Articles