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 all 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
13 changes: 13 additions & 0 deletions README.source.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,19 @@ snippet: Configuring_plugin_using_StorageCredentials

See [`StorageCredentials`](https://docs.microsoft.com/en-us/dotnet/api/microsoft.windowsazure.storage.auth.storagecredentials) for more details.

### Using attachments with Azure Functions

Azure Functions currently has no way to register plugins, these extension methods are a workaround until this feature is added.

To use the extensions, your Function must return (send) or take as parameter (receive) an instance of [`Message`](https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.servicebus.message).

Upload attachment to Azure Storage blob

snippet: Upload_attachment_without_registering_plugin

Download attachment from Azure Storage blob

snippet: Download_attachment_without_registering_plugin

#### Additional providers

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ 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, string messagePropertyToIdentifySasUri = "$attachment.sas.uri") { }
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
25 changes: 25 additions & 0 deletions src/ServiceBus.AttachmentPlugin.Tests/Snippets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,31 @@ void Configuring_plugin_using_StorageCredentials(string connectionString, string
#endregion
}

async Task Upload_attachment_without_registering_plugin(Message message, AzureStorageAttachmentConfiguration config)
{
#region Upload_attachment_without_registering_plugin

//To make it possible to use SAS URI when downloading, use WithBlobSasUri() when creating configuration object
await message.UploadAzureStorageAttachment(config);

#endregion
}
async Task Download_attachment_without_registering_plugin(Message message, AzureStorageAttachmentConfiguration config)
{
#region Download_attachment_without_registering_plugin

//Using SAS URI with default message property ($attachment.sas.uri)
await message.DownloadAzureStorageAttachment();

//Using SAS URI with custom message property
await message.DownloadAzureStorageAttachment("$custom-attachment.sas.uri");

//Using configuration object
await message.DownloadAzureStorageAttachment(config);

#endregion
}

class MyMessage
{
public string MyProperty { get; set; }
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));
}
}
}
39 changes: 39 additions & 0 deletions src/ServiceBus.AttachmentPlugin/MessageExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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 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 = AzureStorageAttachmentConfigurationExtensions.DefaultMessagePropertyToIdentitySasUri)
{
var plugin = new ReceiveOnlyAzureStorageAttachment(messagePropertyToIdentifySasUri);
return await plugin.AfterMessageReceive(message).ConfigureAwait(false);
}
}
}