Skip to content

Commit

Permalink
Support Service Bus resources in Azure Functions (#5593)
Browse files Browse the repository at this point in the history
  • Loading branch information
captainsafia authored Sep 9, 2024
1 parent d8caaec commit 22eae0d
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 2 deletions.
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@
<PackageVersion Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="1.3.2" />
<PackageVersion Include="Microsoft.Azure.Functions.Worker.Extensions.Storage.Blobs" Version="6.6.0" />
<PackageVersion Include="Microsoft.Azure.Functions.Worker.Extensions.Storage.Queues" Version="5.5.0" />
<PackageVersion Include="Microsoft.Azure.Functions.Worker.Extensions.ServiceBus" Version="5.22.0" />
<PackageVersion Include="Microsoft.Azure.Functions.Worker.Extensions.EventHubs" Version="6.3.5"/>
<PackageVersion Include="Microsoft.Azure.Functions.Worker.OpenTelemetry" Version="1.0.0-preview1" />
<PackageVersion Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.4" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<AspireProjectOrPackageReference Include="Aspire.Azure.Storage.Blobs" />
<AspireProjectOrPackageReference Include="Aspire.Azure.Storage.Queues" />
<AspireProjectOrPackageReference Include="Aspire.Azure.Messaging.EventHubs" />
<AspireProjectOrPackageReference Include="Aspire.Azure.Messaging.ServiceBus" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#if !SKIP_EVENTHUBS_EMULATION
using Azure.Messaging.EventHubs;
using Azure.Messaging.EventHubs.Producer;
using Azure.Messaging.ServiceBus;
#endif
using Azure.Storage.Blobs;
using Azure.Storage.Queues;
Expand All @@ -15,6 +16,7 @@
builder.AddAzureBlobClient("blob");
#if !SKIP_EVENTHUBS_EMULATION
builder.AddAzureEventHubProducerClient("eventhubs", static settings => settings.EventHubName = "myhub");
builder.AddAzureServiceBusClient("messaging");
#endif

var app = builder.Build();
Expand Down Expand Up @@ -54,6 +56,13 @@ static string RandomString(int length)
await client.SendAsync([new EventData(data)]);
return Results.Ok("Message sent to Azure EventHubs.");
});
app.MapGet("/publish/asb", async (ServiceBusClient client, CancellationToken cancellationToken, int length = 20) =>
{
var sender = client.CreateSender("myqueue");
var message = new ServiceBusMessage(Encoding.UTF8.GetBytes(RandomString(length)));
await sender.SendMessageAsync(message, cancellationToken);
return Results.Ok("Message sent to Azure Service Bus.");
});
#endif

app.MapGet("/", async (HttpClient client) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<AspireProjectOrPackageReference Include="Aspire.Hosting.Azure.Functions" />
<AspireProjectOrPackageReference Include="Aspire.Hosting.Azure.EventHubs" />
<AspireProjectOrPackageReference Include="Aspire.Hosting.Azure.Storage" />
<AspireProjectOrPackageReference Include="Aspire.Hosting.Azure.ServiceBus" />
<AspireProjectOrPackageReference Include="Aspire.Hosting.AppHost" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@

#if !SKIP_EVENTHUBS_EMULATION
var eventHubs = builder.AddAzureEventHubs("eventhubs").RunAsEmulator().AddEventHub("myhub");
var serviceBus = builder.AddAzureServiceBus("messaging").AddQueue("myqueue");
#endif

var funcApp = builder.AddAzureFunctionsProject<Projects.AzureFunctionsEndToEnd_Functions>("funcapp")
.WithExternalHttpEndpoints()
#if !SKIP_EVENTHUBS_EMULATION
.WithReference(eventHubs)
.WithReference(serviceBus)
#endif
.WithReference(blob)
.WithReference(queue);

builder.AddProject<Projects.AzureFunctionsEndToEnd_ApiService>("apiservice")
#if !SKIP_EVENTHUBS_EMULATION
.WithReference(eventHubs)
.WithReference(serviceBus)
#endif
.WithReference(queue)
.WithReference(blob)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Storage.Blobs" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Storage.Queues" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.ServiceBus" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.EventHubs" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.OpenTelemetry" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Azure.Messaging.ServiceBus;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;

namespace AzureFunctionsEndToEnd.Functions;

public class MyServiceBusTrigger(ILogger<MyServiceBusTrigger> logger)
{
[Function(nameof(MyServiceBusTrigger))]
public void Run([ServiceBusTrigger("myqueue", Connection = "messaging" )] ServiceBusReceivedMessage message)
{
logger.LogInformation("Message ID: {id}", message.MessageId);
logger.LogInformation("Message Body: {body}", message.Body);
logger.LogInformation("Message Content-Type: {contentType}", message.ContentType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Aspire.Hosting.Azure;
/// <param name="name">The name of the resource.</param>
/// <param name="configureConstruct">Callback to configure the Azure Service Bus resource.</param>
public class AzureServiceBusResource(string name, Action<ResourceModuleConstruct> configureConstruct)
: AzureConstructResource(name, configureConstruct), IResourceWithConnectionString
: AzureConstructResource(name, configureConstruct), IResourceWithConnectionString, IResourceWithAzureFunctionsConfig
{
internal List<(string Name, Action<IResourceBuilder<AzureServiceBusResource>, ResourceModuleConstruct, ServiceBusQueue>? Configure)> Queues { get; } = [];
internal List<(string Name, Action<IResourceBuilder<AzureServiceBusResource>, ResourceModuleConstruct, ServiceBusTopic>? Configure)> Topics { get; } = [];
Expand All @@ -30,4 +30,9 @@ public class AzureServiceBusResource(string name, Action<ResourceModuleConstruct
/// </summary>
public ReferenceExpression ConnectionStringExpression =>
ReferenceExpression.Create($"{ServiceBusEndpoint}");

void IResourceWithAzureFunctionsConfig.ApplyAzureFunctionsConfiguration(IDictionary<string, object> target, string connectionName)
{
target[$"{connectionName}__fullyQualifiedNamespace"] = ServiceBusEndpoint;
}
}
11 changes: 10 additions & 1 deletion tests/Aspire.Playground.Tests/ProjectSpecificTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,24 @@ await WaitForAllTextAsync(app,
resourceName: "funcapp",
timeoutSecs: 160);

// Assert that EventHubs triggers work correctly
#if !SKIP_EVENTHUBS_EMULATION
// Assert that EventHubs triggers work correctly
await app.CreateHttpClient("apiservice").GetAsync("/publish/eventhubs");
await WaitForAllTextAsync(app,
[
"Executed 'Functions.MyEventHubTrigger'"
],
resourceName: "funcapp",
timeoutSecs: 160);

// Assert that ServiceBus triggers work correctly
await app.CreateHttpClient("apiservice").GetAsync("/publish/asb");
await WaitForAllTextAsync(app,
[
"Executed 'Functions.MyServiceBusTrigger'"
],
resourceName: "funcapp",
timeoutSecs: 160);
#endif

// TODO: The following line is commented out because the test fails due to an erroneous log in the Functions App
Expand Down

0 comments on commit 22eae0d

Please sign in to comment.