Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Azure Functions #97

Merged
merged 6 commits into from
Mar 15, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
lfalck marked this conversation as resolved.
Show resolved Hide resolved
{
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)
lfalck marked this conversation as resolved.
Show resolved Hide resolved
{
var plugin = new ReceiveOnlyAzureStorageAttachment(messagePropertyToIdentifySasUri);
return await plugin.AfterMessageReceive(message).ConfigureAwait(false);
}
}
}