2023-12-14: The latest ASB SDK doesn't support plugins and requires inheritance. See details in #201.
2021-07-14: Consider using native Azure Service Bus service large messages feature for attachments up-to 100 MB.
This is a plugin for Microsoft.Azure.ServiceBus client
Allows sending messages that exceed maximum size by implementing Claim Check pattern with Azure Storage.
Available here http://nuget.org/packages/ServiceBus.AttachmentPlugin
To Install from the Nuget Package Manager Console
PM> Install-Package ServiceBus.AttachmentPlugin
- Examples
- Convert body into attachment, no matter how big it is
- Sending a message without exposing the storage account to receivers
- Configure blob container name
- Configure message property to identify attachment blob
- Configure custom blob name override
- Configure message property for SAS uri to attachment blob
- Configure criteria for message max size identification
- Configuring connection string provider
- Configuring plugin using StorageCredentials (Service or Container SAS)
- Using attachments with Azure Functions
- Cleanup
- Who's trusting this plugin in production
- Icon
Configuration and registration
var sender = new MessageSender(connectionString, queueName);
var config = new AzureStorageAttachmentConfiguration(storageConnectionString);
sender.RegisterAzureStorageAttachmentPlugin(config);Sending
var payload = new MyMessage
{
MyProperty = "The Value"
};
var serialized = JsonConvert.SerializeObject(payload);
var payloadAsBytes = Encoding.UTF8.GetBytes(serialized);
var message = new Message(payloadAsBytes);Receiving
var receiver = new MessageReceiver(connectionString, entityPath, ReceiveMode.ReceiveAndDelete);
receiver.RegisterAzureStorageAttachmentPlugin(config);
var message = await receiver.ReceiveAsync().ConfigureAwait(false);
// message will contain the original payloadConfiguration and registration with blob SAS URI
var sender = new MessageSender(connectionString, queueName);
var config = new AzureStorageAttachmentConfiguration(storageConnectionString)
.WithBlobSasUri(
sasTokenValidationTime: TimeSpan.FromHours(4),
messagePropertyToIdentifySasUri: "mySasUriProperty");
sender.RegisterAzureStorageAttachmentPlugin(config);Sending
var payload = new MyMessage
{
MyProperty = "The Value"
};
var serialized = JsonConvert.SerializeObject(payload);
var payloadAsBytes = Encoding.UTF8.GetBytes(serialized);
var message = new Message(payloadAsBytes);Receiving only mode (w/o Storage account credentials)
// Override message property used to identify SAS URI
// .RegisterAzureStorageAttachmentPluginForReceivingOnly() is using "$attachment.sas.uri" by default
messageReceiver.RegisterAzureStorageAttachmentPluginForReceivingOnly("mySasUriProperty");
var message = await messageReceiver.ReceiveAsync().ConfigureAwait(false);Default container name is "attachments". The value is available via AzureStorageAttachmentConfigurationConstants.DefaultContainerName constant.
new AzureStorageAttachmentConfiguration(storageConnectionString, containerName:"blobs");Default blob identifier property name is "$attachment.blob". The value is available via AzureStorageAttachmentConfigurationConstants.DefaultMessagePropertyToIdentifyAttachmentBlob constant.
new AzureStorageAttachmentConfiguration(storageConnectionString, messagePropertyToIdentifyAttachmentBlob: "myblob");Default blob name is a GUID.
var sender = new MessageSender(connectionString, queueName);
var config = new AzureStorageAttachmentConfiguration(storageConnectionString)
.OverrideBlobName(message =>
{
var tenantId = message.UserProperties["tenantId"].ToString();
var blobName = $"{tenantId}/{message.MessageId}";
return blobName;
});
sender.RegisterAzureStorageAttachmentPlugin(config);