Skip to content

Commit 617a48f

Browse files
committed
Update Priority Queue Cloud Pattern
Update to .NET 8.0 Updated README
1 parent 820387b commit 617a48f

16 files changed

+238
-158
lines changed
Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFramework>net6.0</TargetFramework>
3+
<TargetFramework>net8.0</TargetFramework>
44
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
5+
<OutputType>Exe</OutputType>
6+
<ImplicitUsings>enabled</ImplicitUsings>
57
</PropertyGroup>
6-
<PropertyGroup>
7-
<DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports>
8-
</PropertyGroup>
9-
<ItemGroup>
10-
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="5.2.0" />
11-
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.0.1" />
12-
</ItemGroup>
138
<ItemGroup>
14-
<None Update="host.json">
15-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
16-
</None>
179
<None Update="local.settings.json">
1810
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
1911
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
2012
</None>
2113
</ItemGroup>
22-
</Project>
14+
<ItemGroup>
15+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
16+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.2" />
17+
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.21.0" />
18+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.ServiceBus" Version="5.17.0" />
19+
</ItemGroup>
20+
</Project>
Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1-
using Microsoft.Azure.WebJobs;
21
using Microsoft.Extensions.Logging;
2+
using Microsoft.Azure.Functions.Worker;
33

44
namespace PriorityQueueConsumerHigh
55
{
6-
public static class PriorityQueueConsumerHighFn
6+
public class PriorityQueueConsumerHighFn
77
{
8-
[FunctionName("HighPriorityQueueConsumerFunction")]
9-
public static void Run([ServiceBusTrigger("messages", "highPriority", Connection = "ServiceBusConnection")]string highPriorityMessage, ILogger log)
8+
private readonly ILogger _logger;
9+
10+
public PriorityQueueConsumerHighFn(ILogger<PriorityQueueConsumerHighFn> logger)
11+
{
12+
_logger = logger;
13+
}
14+
15+
[Function("HighPriorityQueueConsumerFunction")]
16+
public void Run([ServiceBusTrigger("messages", "highPriority", Connection = "ServiceBusConnectionString")] string highPriorityMessage)
1017
{
11-
log.LogInformation($"C# ServiceBus topic trigger function processed message: {highPriorityMessage}");
18+
_logger.LogInformation($"C# ServiceBus topic trigger function processed message: {highPriorityMessage}");
1219
}
1320
}
14-
}
21+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using Microsoft.Extensions.Hosting;
2+
3+
var host = new HostBuilder()
4+
.ConfigureFunctionsWorkerDefaults()
5+
.Build();
6+
7+
host.Run();

priority-queue/PriorityQueueConsumerHigh/local.settings.json

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"IsEncrypted": false,
3+
"Values": {
4+
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
5+
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
6+
"ServiceBusConnectionString": "SERVICE_BUS_CONNECTION_STRING"
7+
}
8+
}
Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFramework>net6.0</TargetFramework>
3+
<TargetFramework>net8.0</TargetFramework>
44
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
5-
<UserSecretsId>736bb6a2-68b4-463b-a8fb-3a90cba7cd4f</UserSecretsId>
5+
<OutputType>Exe</OutputType>
6+
<ImplicitUsings>enabled</ImplicitUsings>
67
</PropertyGroup>
7-
<PropertyGroup>
8-
<DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports>
9-
</PropertyGroup>
10-
<ItemGroup>
11-
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="5.2.0" />
12-
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.0.1" />
13-
</ItemGroup>
148
<ItemGroup>
15-
<None Update="host.json">
16-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
17-
</None>
189
<None Update="local.settings.json">
1910
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
2011
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
2112
</None>
2213
</ItemGroup>
23-
</Project>
14+
<ItemGroup>
15+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
16+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.2" />
17+
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.21.0" />
18+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.ServiceBus" Version="5.17.0" />
19+
</ItemGroup>
20+
</Project>
Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1-
using Microsoft.Azure.WebJobs;
21
using Microsoft.Extensions.Logging;
2+
using Microsoft.Azure.Functions.Worker;
33

44
namespace PriorityQueueConsumerLow
55
{
6-
public static class PriorityQueueConsumerLowFn
6+
public class PriorityQueueConsumerLowFn
77
{
8-
[FunctionName("LowPriorityQueueConsumerFunction")]
9-
public static void Run([ServiceBusTrigger("messages", "lowPriority", Connection = "ServiceBusConnection")]string lowPriorityMessage, ILogger log)
8+
private readonly ILogger _logger;
9+
10+
public PriorityQueueConsumerLowFn(ILogger<PriorityQueueConsumerLowFn> logger)
11+
{
12+
_logger = logger;
13+
}
14+
15+
[Function("LowPriorityQueueConsumerFunction")]
16+
public void Run([ServiceBusTrigger("messages", "lowPriority", Connection = "ServiceBusConnectionString")] string lowPriorityMessage)
1017
{
11-
log.LogInformation($"C# ServiceBus topic trigger function processed message: {lowPriorityMessage}");
18+
_logger.LogInformation($"C# ServiceBus topic trigger function processed message: {lowPriorityMessage}");
1219
}
1320
}
14-
}
21+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using Microsoft.Extensions.Hosting;
2+
3+
var host = new HostBuilder()
4+
.ConfigureFunctionsWorkerDefaults()
5+
.Build();
6+
7+
host.Run();

priority-queue/PriorityQueueConsumerLow/local.settings.json

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"IsEncrypted": false,
3+
"Values": {
4+
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
5+
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
6+
"ServiceBusConnectionString": "SERVICE_BUS_CONNECTION_STRING"
7+
}
8+
}
Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFramework>net6.0</TargetFramework>
3+
<TargetFramework>net8.0</TargetFramework>
44
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
5+
<OutputType>Exe</OutputType>
6+
<ImplicitUsings>enabled</ImplicitUsings>
57
</PropertyGroup>
6-
<PropertyGroup>
7-
<DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports>
8-
</PropertyGroup>
9-
<ItemGroup>
10-
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="5.2.0" />
11-
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.0.1" />
12-
</ItemGroup>
138
<ItemGroup>
14-
<None Update="host.json">
15-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
16-
</None>
179
<None Update="local.settings.json">
1810
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
1911
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
2012
</None>
2113
</ItemGroup>
22-
</Project>
14+
<ItemGroup>
15+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
16+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.2" />
17+
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.21.0" />
18+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.ServiceBus" Version="5.17.0" />
19+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Timer" Version="4.3.0" />
20+
</ItemGroup>
21+
</Project>
Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,34 @@
11
using System;
22
using System.Threading.Tasks;
33
using Azure.Messaging.ServiceBus;
4-
using Microsoft.Azure.WebJobs;
4+
using Microsoft.Extensions.Logging;
5+
using Microsoft.Azure.Functions.Worker;
56

67
namespace PriorityQueueSender
78
{
8-
public static class PriorityQueueSenderFn
9+
public class PriorityQueueSenderFn(ILogger<PriorityQueueSenderFn> logger, ServiceBusClient client)
910
{
10-
[FunctionName("PriorityQueueSenderFunction")]
11-
public static async Task Run(
12-
[TimerTrigger("0,30 * * * * *")] TimerInfo myTimer,
13-
[ServiceBus("messages", Connection = "ServiceBusConnection")] IAsyncCollector<ServiceBusMessage> collector )
11+
private readonly ILogger<PriorityQueueSenderFn> _logger = logger;
12+
private readonly ServiceBusClient _client = client;
13+
14+
[Function("PriorityQueueSenderFunction")]
15+
public async Task Run([TimerTrigger("0,30 * * * * *")] TimerInfo myTimer)
1416
{
17+
var sender = _client.CreateSender("messages");
1518
for (int i = 0; i < 10; i++)
1619
{
1720
var messageId = Guid.NewGuid().ToString();
1821
var lpMessage = new ServiceBusMessage() { MessageId = messageId };
1922
lpMessage.ApplicationProperties["Priority"] = Priority.Low;
2023
lpMessage.Body = BinaryData.FromString($"Low priority message with Id: {messageId}");
21-
await collector.AddAsync(lpMessage);
24+
await sender.SendMessageAsync(lpMessage);
2225

2326
messageId = Guid.NewGuid().ToString();
2427
var hpMessage = new ServiceBusMessage() { MessageId = messageId };
2528
hpMessage.ApplicationProperties["Priority"] = Priority.High;
2629
hpMessage.Body = BinaryData.FromString($"High priority message with Id: {messageId}");
27-
await collector.AddAsync(hpMessage);
30+
await sender.SendMessageAsync(hpMessage);
2831
}
2932
}
3033
}
31-
}
34+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using Microsoft.Extensions.Hosting;
2+
using Microsoft.Extensions.Configuration;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Azure.Messaging.ServiceBus;
5+
6+
var host = new HostBuilder()
7+
.ConfigureFunctionsWorkerDefaults()
8+
.ConfigureAppConfiguration((hostingContext, config) =>
9+
{
10+
config.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true);
11+
})
12+
.ConfigureServices(services =>
13+
{
14+
var configuration = services.BuildServiceProvider().GetRequiredService<IConfiguration>();
15+
16+
services.AddSingleton(configuration);
17+
18+
services.AddSingleton<ServiceBusClient>(sp =>
19+
{
20+
var connectionString = configuration.GetValue<string>("ServiceBusConnectionString");
21+
return new ServiceBusClient(connectionString);
22+
});
23+
})
24+
.Build();
25+
26+
host.Run();

priority-queue/PriorityQueueSender/local.settings.json

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"IsEncrypted": false,
3+
"Values": {
4+
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
5+
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
6+
"ServiceBusConnectionString": "SERVICE_BUS_CONNECTION_STRING"
7+
}
8+
}

0 commit comments

Comments
 (0)