Skip to content

Commit

Permalink
Merge 9d4821a into 2d3eb82
Browse files Browse the repository at this point in the history
  • Loading branch information
lfalck authored Mar 14, 2019
2 parents 2d3eb82 + 9d4821a commit fe09596
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ namespace Microsoft.Azure.ServiceBus
{
System.Threading.Tasks.Task<string> GetConnectionString();
}
public class static MessageExtensions
{
public static System.Threading.Tasks.Task<Microsoft.Azure.ServiceBus.Message> DownloadAzureStorageAttachment(this Microsoft.Azure.ServiceBus.Message message, Microsoft.Azure.ServiceBus.AzureStorageAttachmentConfiguration configuration) { }
public static System.Threading.Tasks.Task<Microsoft.Azure.ServiceBus.Message> DownloadAzureStorageAttachment(this Microsoft.Azure.ServiceBus.Message message) { }
public static System.Threading.Tasks.Task<Microsoft.Azure.ServiceBus.Message> DownloadAzureStorageAttachment(this Microsoft.Azure.ServiceBus.Message message, string messagePropertyToIdentifySasUri) { }
public static System.Threading.Tasks.Task<Microsoft.Azure.ServiceBus.Message> UploadAzureStorageAttachment(this Microsoft.Azure.ServiceBus.Message message, Microsoft.Azure.ServiceBus.AzureStorageAttachmentConfiguration configuration) { }
}
public class PlainTextConnectionStringProvider : Microsoft.Azure.ServiceBus.IProvideStorageConnectionString
{
public PlainTextConnectionStringProvider(string connectionString) { }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
namespace ServiceBus.AttachmentPlugin.Tests
{
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.ServiceBus;
using Xunit;

public class When_using_message_extensions : IClassFixture<AzureStorageEmulatorFixture>
{
readonly AzureStorageEmulatorFixture fixture;

public When_using_message_extensions(AzureStorageEmulatorFixture fixture)
{
this.fixture = fixture;
}

[Fact]
public async Task Should_send_and_receive_with_configuration()
{
var payload = "payload";
var bytes = Encoding.UTF8.GetBytes(payload);
var message = new Message(bytes);
var configuration = new AzureStorageAttachmentConfiguration(
connectionStringProvider: AzureStorageEmulatorFixture.ConnectionStringProvider, containerName: "attachments", messagePropertyToIdentifyAttachmentBlob: "attachment-id");

await message.UploadAzureStorageAttachment(configuration);

Assert.Null(message.Body);

var receivedMessage = await message.DownloadAzureStorageAttachment(configuration);

Assert.Equal(payload, Encoding.UTF8.GetString(receivedMessage.Body));
}

[Fact]
public async Task Should_send_and_receive_with_default_sas_uri_property()
{
var payload = "payload";
var bytes = Encoding.UTF8.GetBytes(payload);
var message = new Message(bytes);
var configuration = new AzureStorageAttachmentConfiguration(
connectionStringProvider: AzureStorageEmulatorFixture.ConnectionStringProvider, containerName: "attachments", messagePropertyToIdentifyAttachmentBlob: "attachment-id")
.WithBlobSasUri();

await message.UploadAzureStorageAttachment(configuration);

Assert.Null(message.Body);

var receivedMessage = await message.DownloadAzureStorageAttachment();

Assert.Equal(payload, Encoding.UTF8.GetString(receivedMessage.Body));
}

[Fact]
public async Task Should_send_and_receive_with_custom_sas_uri_property()
{
var payload = "payload";
var bytes = Encoding.UTF8.GetBytes(payload);
var message = new Message(bytes);
string customSasUri = "$custom-attachment.sas.uri";
var configuration = new AzureStorageAttachmentConfiguration(
connectionStringProvider: AzureStorageEmulatorFixture.ConnectionStringProvider, containerName: "attachments", messagePropertyToIdentifyAttachmentBlob: "attachment-id")
.WithBlobSasUri(customSasUri);

await message.UploadAzureStorageAttachment(configuration);

Assert.Null(message.Body);

var receivedMessage = await message.DownloadAzureStorageAttachment(customSasUri);

Assert.Equal(payload, Encoding.UTF8.GetString(receivedMessage.Body));
}
}
}
49 changes: 49 additions & 0 deletions src/ServiceBus.AttachmentPlugin/MessageExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
namespace Microsoft.Azure.ServiceBus
{
using global::ServiceBus.AttachmentPlugin;
using System.Threading.Tasks;

/// <summary>Extension methods for working with attachments without registering plugin.</summary>
public static class MessageExtensions
{
/// <summary>Upload attachment to Azure Storage blob without registering plugin.</summary>
/// <param name="message"><see cref="Message"/></param>
/// <param name="configuration"><see cref="AzureStorageAttachmentConfiguration"/> object.</param>
/// <returns><see cref="Message"/> with body uploaded to Azure Storage blob.</returns>
public static async Task<Message> UploadAzureStorageAttachment(this Message message, AzureStorageAttachmentConfiguration configuration)
{
var plugin = new AzureStorageAttachment(configuration);
return await plugin.BeforeMessageSend(message).ConfigureAwait(false);
}

/// <summary>Download attachment from Azure Storage blob without registering plugin, using configuration object.</summary>
/// <param name="message"><see cref="Message"/></param>
/// <param name="configuration"><see cref="AzureStorageAttachmentConfiguration"/> object.</param>
/// <returns><see cref="Message"/> with body downloaded from Azure Storage blob.</returns>
public static async Task<Message> DownloadAzureStorageAttachment(this Message message, AzureStorageAttachmentConfiguration configuration)
{
var plugin = new AzureStorageAttachment(configuration);
return await plugin.AfterMessageReceive(message).ConfigureAwait(false);
}

/// <summary>Download attachment from Azure Storage blob without registering plugin, using default message property to identify SAS URI.</summary>
/// <param name="message"><see cref="Message"/></param>
/// <returns><see cref="Message"/> with body downloaded from Azure Storage blob.</returns>
public static async Task<Message> DownloadAzureStorageAttachment(this Message message)
{
var plugin = new ReceiveOnlyAzureStorageAttachment(AzureStorageAttachmentConfigurationExtensions.DefaultMessagePropertyToIdentitySasUri);
return await plugin.AfterMessageReceive(message).ConfigureAwait(false);
}


/// <summary>Download attachment from Azure Storage blob without registering plugin, with custom message property to identify SAS URI.</summary>
/// <param name="message"><see cref="Message"/></param>
/// <param name="messagePropertyToIdentifySasUri">Message property which contains the SAS URI used to fetch message body from blob.</param>
/// <returns><see cref="Message"/> with body downloaded from Azure Storage blob.</returns>
public static async Task<Message> DownloadAzureStorageAttachment(this Message message, string messagePropertyToIdentifySasUri)
{
var plugin = new ReceiveOnlyAzureStorageAttachment(messagePropertyToIdentifySasUri);
return await plugin.AfterMessageReceive(message).ConfigureAwait(false);
}
}
}

0 comments on commit fe09596

Please sign in to comment.