/ServiceBus.AttachmentPlugin

Microsoft Azure Service Bus attachment plugin

Primary LanguageC#MIT LicenseMIT

Icon

This is an add-in for Microsoft.Azure.ServiceBus client

Allows sending messages that exceed maximum size by implementing Claim Check pattern with Azure Storage.

license develop opened issues

Nuget package

NuGet Status

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

Configuration and registration

var sender = new MessageSender(connectionString, queueName);
var config = new AzureStorageAttachmentConfiguration(storageConnectionString);
sender.RegisterAzureStorageAttachmentPlugin(config);

Sending

var payload = new MyMessage { ... }; 
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 msg = await receiver.ReceiveAsync().ConfigureAwait(false);
// msg will contain the original payload

Sending a message without exposing the storage account to receivers

Configuration and registration with SAS uri

var sender = new MessageSender(connectionString, queueName);
var config = new AzureStorageAttachmentConfiguration(storageConnectionString)
	.WithSasUri(sasTokenValidationTime: TimeSpan.FromHours(4), messagePropertyToIdentifySasUri: "mySasUriProperty");
sender.RegisterAzureStorageAttachmentPlugin(config);

Sending

var payload = new MyMessage { ... }; 
var serialized = JsonConvert.SerializeObject(payload);
var payloadAsBytes = Encoding.UTF8.GetBytes(serialized);
var message = new Message(payloadAsBytes);

Receive

var receiver = new MessageReceiver(connectionString, entityPath, ReceiveMode.ReceiveAndDelete);
var msg = await receiver.ReceiveAsync().ConfigureAwait(false);
// msg will contain the original payload

Receiving only mode (w/o Storage account credentials)

// Overide message property used to identify SAS URI
// .RegisterAzureStorageAttachmentPluginForReceivingOnly() is using "$attachment.sas.uri" by default
var receiver = messageReceiver.RegisterAzureStorageAttachmentPluginForReceivingOnly("mySasUriProperty");
var msg = await receiver.ReceiveAsync().ConfigureAwait(false);

Configure blob container name

Default container name is "attachments".

new AzureStorageAttachmentConfiguration(storageConnectionString, containerName:"blobs");

Configure message property to identify attachment blob

Default blob identifier property name is "$attachment.blob".

new AzureStorageAttachmentConfiguration(storageConnectionString, messagePropertyToIdentifyAttachmentBlob: "myblob");

Configure message property for SAS uri to attachment blob

Default SAS uri property name is "$attachment.sas.uri".

new AzureStorageAttachmentConfiguration(storageConnectionString).WithSasUri(messagePropertyToIdentifySasUri: "mySasUriProperty");

Configure criteria for message max size identification

Default is to convert any body to attachment.

// messages with body > 200KB should be converted to use attachments
new AzureStorageAttachmentConfiguration(storageConnectionString, message => message.Body.Length > 200 * 1024);

Configuring connection string provider

When Storage connection string needs to be retrieved rather than passed in as a plain text, AzureStorageAttachmentConfiguration accepts implementation of IProvideStorageConnectionString. The plugin comes with a PlainTextConnectionStringProvider and can be used in the following way.

var provider = new PlainTextConnectionStringProvider("connectionString");
var config = new AzureStorageAttachmentConfiguration(provider);

Additional providers

Who's trusting this add-in in production

Microsoft Codit

Proudly list your company here if use this add-in in production

Icon

Created by Dinosoft Labs from the Noun Project.