Getting Started
Architecture
NServiceBus
Transports
Persistence
Hosting
ServiceInsight
ServicePulse
ServiceControl
Monitoring
Modernization
Samples

Azure Blob Storage DataBus

NuGet Package: NServiceBus.DataBus.AzureBlobStorage 7.x
Target Version: NServiceBus 10.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 that is larger than the allowed 4MB by the transport. 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:

[TimeToBeReceived("00:03:00")]
public class MessageWithLargePayload :
    ICommand
{
    public ClaimCheckProperty<byte[]> LargePayload { get; set; }
    public string Description { get; set; }
}

ClaimCheckProperty<T> is an NServiceBus data type that instructs NServiceBus not to treat the LargePayload property in the NServiceBus normal flow.

When sending a message using the NServiceBus DataBus mechanism, the message payload is stored in Azure Storage as a blob. The value assigned to the property while the message is being transported is a special value that allows the message's property to reconnect with its original value once the 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",
	"LargePayload":
	{
		"Key":"7c9a4430-c020-4462-a849-9994f3de354a",
		"HasValue":true
	}
}

Key represents the Azure Storage blob name used to store the message property's original value.

Configuring the DataBus location

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

var blobServiceClient = new BlobServiceClient("UseDevelopmentStorage=true");

var claimCheck = endpointConfiguration.UseClaimCheck<AzureClaimCheck, SystemJsonClaimCheckSerializer>()
    .Container("testcontainer")
    .UseBlobServiceClient(blobServiceClient);

Attachment blobs will be found in the 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 ClaimCheckProperty<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

This is the receiving message handler:

public class MessageWithLargePayloadHandler(ILogger<MessageWithLargePayloadHandler> logger) :
    IHandleMessages<MessageWithLargePayload>
{

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

Samples

Related Articles

  • Claim Check (DataBus)
    How to handle messages that are too large to be sent by a transport natively by using NServiceBus Claim Check.